- Home /
How would I search my scene for a tag
I have a scene with multiple enemies but I want a script to check for the last enemy if there are no more enemies then I want to load a new scene
Answer by dacarrera · Nov 11, 2019 at 04:13 AM
To search for tags use FindGameObjectsWithTag (and make sure this tag exists)
GameObject.FindGameObjectsWithTag("Enemy");
This will return a GameObject[] array. If there aren't any gos with the tag, it'll return an empty array. You can just check if it is empty and if so, load your new scene
checkThis = GameObject.FindGameObjectsWithTag("YourTagHere");
if(checkThis.Length == 0)
{
// empty array was returned, load your new scene here
}
else
{
Debug.Log("There are still " + checkThis.Length + " enemies in the scene");
}
So would the first box be in the start function and the second box be in the update function and for "checkThis" what would I replace it for
It depends on how often you want this called. If you want to check for enemies in Update, you should make a placeholder like private GameObject[] remainingEnemies
outside of your method and then make a function like the above in Update()
.
If you put the first checkThis = GameObject.FindGameObjectsWithTag("YourTagHere");
part in Start() only, it'll only be assigned once. You'd want this assignment called everything you're checking if enemies are left.
I can give you a fleshed out example if you're still stuck!
Ya still not working unfortunately um I think it has something to do If(checkThis.Lenth == 0)
Your answer
Follow this Question
Related Questions
GameObject references runtime script in scene file. Unsure whats wrong 2 Answers
List objects in a scene file 0 Answers
How to access a game object from a different scene? 0 Answers
How do I play gameobjects in my scene from one script / Script is not working 0 Answers
Saving Hierarchy to file 1 Answer