I am building an airplain game where you fly around the city destroying tanks and other airplains to win the game. My question is how would i go about making a script that once I destroy all three tanks/plains, it go to game over. All my enemy are tagged enemy. I really dont have any starting code because im not sure how to start this one.
You could have an empty gameobject with a script just to check the game over conditions. You could mantain a variable in this script with the maximum number of enemies for your level. Whenever you create an enemy, you subtract one from that variable. Whenever one of the active enemies are destroyed check if that variable is greater than zero. If not, you can load another scene with a proper game over GUI or just enable some overlaying GUI on that same scene.
This assumes you know exactly how many enemies there will be in a given scene.
-
First create a script call it “GameWinner”(bad name, I know)
-
In the script you write this:
static var destroyedEnemies = 0; //Current number of destroyed enemys var numberOfEnemies : int; //Fill this in the Inspector. function Update (){
if(destroyedEnemies >= numberOfEnemies){
//End the game here. } }
-
When you destroy/kill your enemies you write:
GameWinner.destroyedEnemies++; // This adds to the variable “destroyedEnemies” in the “GameWinner” script.
I hope this helps, please tell if you need some help or clarification. I’ll be happy to help.
P.S I am sorry for the poorly formatted code, it would(for some reason) not format it properly.