- Home /
How can I change scene after i've destroyed all my game objects?
This is more a question towards letting unity know i've destroyed all the targets inside my game. I know I can use appllication.loadlevel to change scene but I don't know how to make it so that unity knows to change the scene once all the targets are destroyed. Right now i'm using tags on my targets. Please help! thank you!
Answer by robertbu · Oct 23, 2013 at 04:57 PM
Try this in Update():
if (GameObject.FindGameObjectsWithTag("SomeTag").Length == 0)
Application.LoadLevel("SomeLevel");
For a reasonable number of game object and in most game situations, calling FindGameObjectsWithTag() every frame should be fine. If not, you can use InvokeRepeating() to call it less frequently (like 5 or 10 times a second), or you can do a more efficient solution by tracking these game objects yourself and using a count like @meat5000 suggests.
If you know how many objects you have you can simply -- (decrement) an int variable storing that quantity. When it reaches 0 call loadlevel
Your answer
