Scene change!

Short question:

How would i make it that when all of the enemy entity’s are destroyed from the scene it changes to another scene.

My game will be to Destroy all of the enemys to bring light to the world, i was going to switch to a new scene which is bright and fixed :slight_smile:

The easiest way is to execute a check once an enemy dies to see if there are other enemies left. If not, new scene is loaded. You should of course make sure to keep track of your enemies by e.g. filling a list on startup or similar.

Thanks, i need to figure out how to do this, i am not the best programmer

Would this work to switch scenes?

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
     
    Application.LoadLevel(2)//change scene
     
    }
    }

If so, what script would i put this in and where to attach it?

That would work, but I would suggest using InvokeRepeat(). So instead of using Update() which calls every 33ms (I think) call it every second with InvokeRepeat(). This will save a lot of processing power,

Edit. @ what, Glockenbeat suggested is the best method probably. You would need to have a manager that has a function that is called, when an enemy dies. Each enemyScript, should know when it dies. It should also know its manager (use GetComponent) then when enemy does, in the enemy script, tell the manager an enemy has died. Lower your enemyCounter. If enemy count == 0, load new scene.

Thanks for the help, i don’t need the power for this game :stuck_out_tongue: uhm, i figured it out but i cannot tag an object with “Enemy”, i go to where it says ‘Untagged’ and try to tag it but no where i can write my own tag? any ideas, it works as i tested it with “Respawn”

Another easy way you could do this is to have a static variable in your enemy script. On Start you would increment this int. Then when each ones dies you can decrement it and when it hits 0 load you new scene. Keep in mind you should set it to 0 if you exit the game scene and restart without killing all the enemy’s.

public static int enemyCount=0;

public void Start(){
   enemyCount++;
}

public void Die(){
   enemyCount--;
   if (enemyCount==0){
      //Load new scene here
   }

}

You can set your own tag right away in this dropdown. Just at the bottom of the list, there is a button called “Add new tag” or sth.

Then i cannot enter my own string…?

Update happens every frame. If you’re running at 30FPS then that’d be every 33ms, but if you’re running at 60FPS it’s every 16.6ms, and so on (frame time in ms = 1000 / frames per second).

Look at the very top of the inspector, there should be a ‘Tags’ popout. You can open that and then add your own strings in there.

I have found the drop down menu and it does not let me enter my own strings?

You have to go to ‘Add Tag’ at the bottom, like asd234w4r5 said.

I have done.

Well help us to help you. Post a screenshot or something. Describe what you’re doing in more detail.

Okay, sorry :slight_smile:

1220574--50273--$tagging.jpg

That appears once i click ‘add tag’, uhm? :slight_smile:

Thanks for being a helpful community :slight_smile:

I have clicked that and yet no where is a place to add my own tag?

Ah, thanks a bunch :), Can i rep you somewhere on here? - For your help.