Skip to content

Instantly share code, notes, and snippets.

@ZeredaGames
Created February 23, 2019 11:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZeredaGames/0f9499f1edca8f37e336d4a6d3b41986 to your computer and use it in GitHub Desktop.
Save ZeredaGames/0f9499f1edca8f37e336d4a6d3b41986 to your computer and use it in GitHub Desktop.
Answers Help script
namespace ZeredaGamesEngine.UnityAwnsers.Help.Examples{
///<summary>
/// A basic tutorial script on how to spawn a prefab via code.
///</summary>
public class SpawnPrefab: MonoBehaviour{
[Header("Final Rendition - SpawnPrefab Script Class By Zereda-Games"),Space,Tooltip("The tag on the health text component we want to display on- 'Text Component'.[ Notes for Unity Awnsers.com] anyone who would like to change this post for future unity changes I give mediators full rights to do so.The script really is basic, and first write-up only took around 45 min. Editing took a few days but that's just because there are a ton of questions not many people see these post before they get buried. I made this with all of you by the comments for DanProd by request, it was a simple micro-challenge I took on to test my skill's. I started out on Udemy in 2015 and have been writing c# as much as i can for the past 3 years... If you are using this script in your kit, all i ask is to give the following credit. DanProd - For starting this thread, asking the question, and supplying the basic's of what he wanted to do, sean244 and James_BadAlchemy - Gave suggestions that were taken into consideration in the write-up, HomebrewAI - Corrected mistakes done by me, ZeredaGames, and finally Me the writer, Thamas Bell @ ZeredaGames & Co. BTW You can remove this Blurb from the tool tip in your inspector. just give credit and change as needed"),Space,Header("Public Settings:")]
public string TagOnHealthText="HealthTxt";
[Tooltip ("The tag on the position text component we want to display on")]
public string TagOnPositionText="PlayerPositionTxt";
[Tooltip ("Name of the prefab in resources."),TextArea(0,2)]
public string NameOfPrefab="BloodSplatterPrefab";
[Space,Tooltip ("How Long do we want our object to stay in the world before they destroy?")]
public float WaitTime;
[Space,Tooltip ("Value is divided by the WaitTime so that the instantiated prefab on death will destroy before the game object thus solving any unwanted continued Coroutine.")]
public float PrefabDestroyMultiplier;
[Space, Tooltip ("Do we want the object to destroy right away?")]
public bool InstantDestroy=false;
[Space,Header("Private Settings:"),Space,SerializeField]
private static int maxHealth = 100;
[Space, SerializeField]
private static int currentHealth = 100;
[SerializeField]
private static Text HealthText;
[SerializeField]
private static Text PositionText;
[SerializeField]
private static Transform transform;
[SerializeField]
private static GameObject go
void Start()
{
PositionText = GameObject.FindWithTag(TagOnPositionText + "PositionText").GetComponent<Text >();
HealthText = GameObject.FindWithTag(TagOnHealthText).GetComponent<Text >();
currentHealth = maxHealth;
HealthText.text = currentHealth.ToString();
PositionText.text = "X"+transform.position.x+"/Y"+transform.position.y+"/Z"+transform.position.z;
}
void Update()
{
HealthText.text = currentHealth.ToString();
Debug.Log( "Current HP: "+HealthText.text);
PositionText.text = "X"+transform.position.x+"/Y"+transform.position.y+"/Z"+transform.position.z;
Debug.Log( "Position: "+PositionText.text);
if (Equals(currentHealth, 0))
{
gameObject.Instatiate(deathPrefab,transform.transform,Quatinion.identity);
transform= go.transform;
if(Equals(go ,null)){
//Old way Depricated in newer Unity's
//go = (GameObject)Resource.Load(NameOfPrefab);
//New Way
go = Resource.Load<GameObject>(NameOfPrefab);
gameObject.Instatiate(go,transform.transform,Quatinion.identity);
go = this;
transform= go.transform;
}
}
if(EqualsPlayer.hp,=0) && Equals(WaitTime<=0.01f) && !Equals(InstantDestroy,true)){
StartCoroutine (DestroyPlayer(this.gameObject), WaitTime);
StartCoroutine (DestroyPrefab(go), WaitTime/PrefabDestroyMultiplier);
}
else if(EqualsPlayer.hp,=0) && Equals(WaitTime,0.0f) || EqualsPlayer.hp,=0) && Equals(InstantDestroy,true)){
Destroy(this.gameObject);
StartCoroutine (DestroyPrefab(go), WaitTime/PrefabDestroyMultiplier);
}
}
//Statics are used to call directly in other code and to make menu buttons
public static void DamagePlayer((Vector3 direction, int Hurt)
{
currentHealth -= Hurt;
}
//Statics are used to call directly in other code and to make menu buttons
public static void HealPlayer(Vector3 direction, int Aid)
{
currentHealth += Aid;
}
//Public's must have 1 parameter to use as a unity button I tried this to confirm.
public void DamagePlayer(int Hurt)
{
currentHealth -= Hurt;
}
//Public's must have 1 parameter to use as a unity button I tried this to confirm.
public void HealPlayer(int Aid)
{
currentHealth += Aid;
}
IEnumerator DestroyPlayer(Player player,float waitTime){
yield return new WaitForSecond(waitTime);
Destroy(player);
StopCoroutine (DestroyPlayer());
}
IEnumerator DestroyPrefab(GameObject prefab,float waitTime){
yield return new WaitForSecond(waitTime);
Destroy(prefab);
StopCoroutine (DestroyPrefab());
}
IEnumerator DestroyPrefab(System.Object prefab,float waitTime){
yield return new WaitForSecond(waitTime);
Destroy(prefab);
StopCoroutine (DestroyPrefab());
}
IEnumerator DestroyPrefab(object prefab,float waitTime){
yield return new WaitForSecond(waitTime);
Destroy(prefab);
StopCoroutine (DestroyPrefab());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment