- Home /
Change level after destroying all enemies?
Ok, I'm new in Unity3D and I've made some simple game where you are destroying cubes as enemies.. But now I need to change level after I destroy all cubes.
Do I need to add some variable to count cubes as objects? How to do it easiest way?
Thank you..
Answer by DavidD · Aug 18, 2012 at 03:59 PM
I don't know which language you're using so I'll go with C# because I am familiar with that.
You could either use a list or a simple variable. Whenever you add an enemy to the world you increment the variable (or add it to the list) and whenever the variable reaches 0 (or the list is empty) you change level. Of course you would also decrement the variable (or remove the enemies from the list) when they are destroyed.
You could put this script on either your main character or in some sort of "master object" if you have one of those. If not the main character will do fine.
Ok, I use now javascript but I can handle C#.. I created array of objects but they don't desepear from list? Am I missing something? Can you write down few lines of code?
At the beginning of your script which you attach to the main character you'd declare an int called "enemyAmount" or something like that and set it to 0.
Then if you add enemies through the editor, give them a tag like "enemy" and then you could do "enemyAmount = GameObject.FindGameObjectsWithTag("enemy").length" to get the amount of enemies (keep in $$anonymous$$d this is C# but you should get the gist)
Now, whenever the player kills an enemy you decrement enemyAmount by typing enemyAmount--. Then check if enemyAmount == 0, if so you change level.
Hope you understand that!
Ok, I managed it :)
Here is code I used
pragma strict
var level:int;
function Start () {
}
function Update () { print(GameObject.FindGameObjectsWithTag("YOUR_TAG").Length);
if(GameObject.FindGameObjectsWithTag("YOUR_TAG").Length==0) {Application.LoadLevel(level);}
}
And I attached it to main characater..