- Home /
Instantiate prefab once help C#
Hi, I'm trying to spawn a health object when the players health is 15 or less but my instantiate creates a loop if I don't pick up the health object instantly. Could anyone help me please?
Code lines: 14, 15, 30, 31, 32, 33, 34, 35, 36, 37
 using UnityEngine;
 using System.Collections;
 
 public class Player_Health : MonoBehaviour {
 
     public static float curHealth = 100f;
     public static float maxHealth = 100f;
     public static float decHealth = 5f;
     public static float regHealth = 1f;
 
     public static bool levelFail = false;
     public GUISkin skin;
 
     public GameObject spawnHealth;
     public bool completedHealth;
 
     void Start(){
         curHealth = 1;
         }
 
     void Update(){
         if (curHealth < maxHealth) 
         {
             curHealth += regHealth * Time.deltaTime;
         }
         if (curHealth <= 0) 
         {
             levelFail = true;
         }
         if (curHealth <= 15) 
         {
             completedHealth = true;
             if(completedHealth = true)
             {
                 Instantiate(spawnHealth);
                 completedHealth = false;
             }
 
         }
     }
 
     void OnTriggerStay2D(Collider2D other){
         if (other.gameObject.tag == "Darkness") 
         {
             curHealth -= decHealth * Time.deltaTime;
         }
     }
     void OnTriggerEnter2D(Collider2D other){
         if (other.gameObject.tag == "Health") 
         {
             Player_Health.curHealth = maxHealth;
             Destroy(other.gameObject);
         }
     }
 
 
     void OnGUI(){
         GUI.skin = skin;
         string HealthText = "Your Health: " + curHealth + " / " + maxHealth;
         GUI.Box (new Rect(Screen.width /2 - 350, 20, 200, 24), HealthText);
 
         if (levelFail == true)
         {
             Time.timeScale = 0;
             Rect winScreenRect = new Rect(Screen.width/2 - (Screen.width *.5f/2), Screen.height/2 - (Screen.height *.5f/2), Screen.width *.5f, Screen.height *.5f);
             GUI.Box(winScreenRect, "Level Failed!");
             
             if (GUI.Button(new Rect(winScreenRect.x + winScreenRect.width - 170, winScreenRect.y + winScreenRect.height - 60, 150, 40), "Retry"))
             {
                 Destroy(gameObject);
                 Application.LoadLevel(Application.loadedLevel);
                 Game_Manager.lightsActive = 0;
                 curHealth = 100f;
                 levelFail = false;
                 Time.timeScale = 1;
                 EndLvl.curHealthGuard = EndLvl.maxHealthGuard;
             }
             if (GUI.Button(new Rect(winScreenRect.x + 20, winScreenRect.y + winScreenRect.height - 60, 100, 40), "Quit"))
             {
                 Application.LoadLevel(0);
             }
             
         }
     }
 
 }
 
The problem--I am assu$$anonymous$$g--is that the health object is being constantly spawn. If that is the case (which the code seems to show), the fix would be to remove: completedHealth = true; in the curHealth 15.
Yeah, this here:
  if(completedHealth = true)
 
is not checking for equivalency, it is assigning true to completedHealth, then it is evaluating the result of that as a boolean expression (which will be true).
In the future, make sure you are using == if you want to check for equivalency. Also, you totally don't have to check for the equivalency of a bool value. Just use
 if(completedHealth)  
or
 if(!completedHealth)
Answer by MarksTeq · Oct 23, 2014 at 01:02 AM
Also change
  if (curHealth <= 15) 
to
 if (curHealth <= 15 && completedHealth == true)
 {
  Instantiate(spawnHealth);
 completedHealth = false;
 } 
and do this:
  void Start(){
 curHealth = 1;
 completedHealth = true;
 }
else it's just going to repeat infinitely because you always just set it back to true
Thank you very much for your help, it's all working now.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                