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);
}
}
}
}