- Home /
I can't figure out why this if statement isn't responding
I'm following a tutorial online and I've run into an issue. I've checked several times to see ensure I followed the instructions correctly. I've checked the comments and even recent comments are complimenting the tutorial without having had the same issue.
bool EnemyIsAlive()
{
//
searchCountdown -= Time.deltaTime;
if (searchCountdown <= 0f)
{
searchCountdown = 1f;
Debug.Log("Searching!");
if (GameObject.FindGameObjectsWithTag("Enemy") == null)
{
Debug.Log("All enemies have been eliminated!");
return false;
}
}
return true;
}
The search countdown is resetting and I'm getting the "Searching!" message just fine but the bool does not return false and the "All enemies have been destroyed" message is not displayed. I've double checked that the instantiated objects with the Enemy tag are being destroyed and that there are no other objects in the scene with the tag. is there something wrong with the if statement I'm just not seeing?
Answer by sacredgeometry · Dec 30, 2020 at 06:48 AM
As per the documentation for FindGameObjectsWithTag:
Returns an array of active GameObjects tagged tag. Returns empty array if no GameObject was found. Tags must be declared in the tag manager before using them. A UnityException will be thrown if the tag does not exist or an empty string or null is passed as the tag.
https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html
i.e. it doesn't return null if there are no gameobjects with the tag only when the tag doesnt exist.
Your answer
Follow this Question
Related Questions
Need help accessing booleans from other script. 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
bool doesn't change?? 0 Answers