- Home /
Win game based on destroying enemys
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.
Answer by FLASHDENMARK · Dec 05, 2011 at 06:27 PM
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.
For some reson this is playing end game instantly on play. I placed this on my tank >
GameWinner.destroyedEnemies++;
I have set it to is trigger.
I have this on my plain.
static var destroyedEnemies = 0; //Current number of destroyed enemys
var numberOfEnemies : int; //Fill this in the Inspector.
function Update (){
if(destroyedEnemies >= numberOfEnemies){
Application.LoadLevel("End Game");
}
}
Answer by cj_coimbra · Dec 05, 2011 at 06:21 PM
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.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Rest postion after animation plays 0 Answers
Spawning portal 1 Answer
Making a Jetpack 3 Answers
Convert JS to C# 1 Answer