x


End level when all enemies eliminated

I am making a simple FPS and I want to transfer the player to the next level (scene) after all of the enemies are eliminated. How am I supposed to do this? I am a beginner to Unity, and I only understand a very basic Idea of scripting. Please tell me the script, and where, or how, to put it.

Thank You SO MUCH in advance!

more ▼

asked Apr 03 '11 at 07:22 AM

UNKNOWN or whatever gravatar image

UNKNOWN or whatever
5 10 11 14

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

You could make use of a game controller to count the number of enemies in your scene by having them register with the controller when they enable and unregister when they disable. When you reach a count of 0, you can load a new level.

To do this, find or create an empty game object in your scene tagged GameController (by default, there is none).

On this game object with the tag GameController, add the following ObjectivesManager.

ObjectivesManager.js

private var objectives : int;

function OnEnableObjective() {
    objectives++;
}

function OnDisableObjective() {
    objectives--;
    if (objectives == 0)
        SendMessage("OnObjectives");
}

function OnObjectives() {
    Application.LoadLevel(Application.loadedLevel + 1);
}

Then, on every enemy you should have code like this. You want to let the controller know that an objective was enabled and disabled at key moments.

Objective.js

function OnEnable() {
    var gameController = GameObject.FindGameObjectWithTag("GameController");
    gameController.SendMessage("OnEnableObjective");
}

function OnDisable() {
    var gameController = GameObject.FindGameObjectWithTag("GameController");
    gameController.SendMessage("OnDisableObjective");
}
more ▼

answered Apr 03 '11 at 11:53 AM

Statement gravatar image

Statement ♦♦
20.2k 35 71 176

thanks statement, but it seems like it is not working. i think the script doesnt disable after the enemy dies

Apr 05 '11 at 09:45 AM UNKNOWN or whatever
(comments are locked)
10|3000 characters needed characters left
function Update() {
    var enemies = GameObject.FindWithTag("enemy") //find gameobjects with the tag "enemy" (you must therefore tag the enemies with "enemy"
    if(enemies == null){ //if no gameobjects with the tag "enemy" are found

    //change scene

    }
}

not certain this will work as I'm away from unity at the moment but basically it finds all the gameobjects with the tag "enemy", if it doesn't find any then you can put in a script to change the scene

hopefully its useful

Scribe

more ▼

answered Apr 03 '11 at 09:02 AM

Scribe gravatar image

Scribe
1.8k 12 16 34

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3418
x744
x329

asked: Apr 03 '11 at 07:22 AM

Seen: 1458 times

Last Updated: Apr 03 '11 at 07:22 AM