bool not properly working and time counter only adding once
Hey, I'm trying to have a counter after chopping down a tree, so I can spawn it again after a couple of seconds, but the problem is that the IF test is not reacting even though the bool is set to true...
using UnityEngine;
using System.Collections;
public class mineTreeStaticMap : MonoBehaviour {
GameObject manager;
GameMangerINV managerScript;
public int treeHealth = 100;
public int currentTreeHealth;
public float time = 0;
public float timeToSpawn = 10;
public bool treeDead = false;
void Start () {
manager = GameObject.Find("Player"); //GearMaster
managerScript = manager.GetComponent<GameMangerINV>();
currentTreeHealth = treeHealth;
treeDead = false;
}
void Update () {
if (currentTreeHealth <= 0) {
gameObject.SetActive(false);
treeDead = true;
}
if(treeDead == true){
time = time + Time.deltaTime;
Debug.Log ("TIMER:" + time);
if(time >= timeToSpawn) {
gameObject.SetActive(true);
currentTreeHealth = treeHealth;
time = 0f;
}
}
}
public void getDamage(int dmg) {
print ("fant");
currentTreeHealth = currentTreeHealth - dmg;
//healthslider.value = treeHealth;
}
}
Also if you have any recommendations, I would gladly take them. I'm worried about the game lagging when it counts in every frame.
Thanks.
Answer by TBruce · May 05, 2016 at 12:55 AM
This should do it for you
using UnityEngine;
using System.Collections;
public class mineTreeStaticMap : MonoBehaviour
{
GameObject manager;
GameMangerINV managerScript;
public int treeHealth = 100;
public int currentTreeHealth;
public float time = 0;
public float timeToSpawn = 10;
public bool treeDead = false;
void Start ()
{
manager = GameObject.Find("Player"); //GearMaster
managerScript = manager.GetComponent<GameMangerINV>();
currentTreeHealth = treeHealth;
}
void Update ()
{
if ((currentTreeHealth <= 0) && (!treeDead))
{
gameObject.SetActive(false);
treeDead = true;
StartCoroutine(StartTimer());
}
}
IEnumerator StartTimer()
{
yield return new WaitForSeconds(timeToSpawn);
currentTreeHealth = treeHealth;
treeDead = false;
gameObject.SetActive(true);
}
public void getDamage(int dmg)
{
print ("fant");
currentTreeHealth = currentTreeHealth - dmg;
//healthslider.value = treeHealth;
}
}
Thank you, could you or anyone elaborate some on what was wrong, so I can learn from my mistake? Especially the boolean thing...
Thanks again!
The idea is this when a tree has been chopped down you want to
Set the tree to dead
Deactivate the tree (hide the tree)
Start a countdown timer
As long as the countdown timer is greater than 0 the will tree stay inactive and hidden
When the countdown timer reaches 0 you want to activate the tree, mark it not dead and show the tree
In Update this if block will only be entered if the currentTreeHealth is less than or equal to 0 and the tree has not been marked as dead
if ((currentTreeHealth <= 0) && (!treeDead))
Here (within the if block)
gameObject.SetActive(false);
treeDead = true;
StartCoroutine(StartTimer());
we
Deactivate the tree (which hides it)
Set the boolean treeDead to true (so that the if block is not entered)
Start a countdown timer (StartCoroutine(StartTimer());) which is a Coroutine
IEnumerator StartTimer() { yield return new WaitForSeconds(timeToSpawn); currentTreeHealth = treeHealth; treeDead = false; gameObject.SetActive(true); } This statement will yield from any further code execution until timeToSpawn number of seconds is complete
yield return new WaitForSeconds(timeToSpawn); Once timeToSpawn number of seconds is complete we
Set currentTreeHealth to the default treeHealth value
$$anonymous$$ark the tree as alive (treeDead = false;)
Show the tree (gameObject.SetActive(true);)
Well explained, thanks a lot! I appreciate that you took time to explain!
Your answer
Follow this Question
Related Questions
how do i say this 1 Answer
How do I make a gun reload after 30 shots? 4 Answers
Get Time A Bool Has Been True 4 Answers
Three button combination to open next scene 2 Answers
Why doesn't this if-statement work? 1 Answer