- Home /
Loading new level after killing enemies
Hello i just started using unity3d, and i have some basic javascript expirience,what i want is when i kill all my enemies a new level should load, i used this:
function OnCollisionStay (col : Collision)
{
if(col.gameObject.name == "Bullet(Clone)")
{
Application.LoadLevel("menu");
Destroy(col.gameObject);
Destroy(gameObject);
}
}
it worked but it will load new level after killing only one enemy. How can i make it to load a new level after killing all 6 enemies?
okay, within all these codes im a bit lost, is there any way to make new level load after having like 60 points (to get 10 points for each enemies) ?
Converted your "answer" to comment.
And yes, using points like that would just be easier in general, but... you would have to store the points somewhere else, not in the enemy or bullet, but say on the player. And you would have to access the player scripts from inside the enemy code and add points.
That should technically be on it's own question.
As for getting confused due to the answers I suggest either going with the one that makes the most sense to you, or go one by one and test them out. See if one suits your needs more then another.
As for the points it would really be the same concept. Ins$$anonymous$$d of seeing how many enemies are left, you would see if you have gotten enough points.
Only difference is every time you kill an enemy you will add 10 points.
ok, so i can add 10 points after killing an enemy, but if i made that in enemy_collision script how do i "connect" it to the script on my player where i store the points?
Look at the large link in my comment. You need to use getcomponent. I normally code in c# so I can't write up an example for you in js, that's why I linked the manual page for it :)
Answer by trs9556 · Jul 06, 2013 at 08:09 PM
You can check if any more enemies exist, and if no more exist then you load the new level.
I don't know the name of your enemy, and it's possible you might have more then 1 enemy name so it would be beneficial to do your check by tag.
so for starters create a new tag, to do this click on any GameObject and in the inspector there is a drop down menu that says "Tag". It is located right under the GameObject's name. Click that and go to the button and click "Add Tag...".
Under the tag array increase the size of the array by 1, and then edit the last index and put the tag "enemy". This is done by clicking to the right of the "Element" (pretty fat right to be exact.).
Once this is done change your collision to this:
if(col.gameObject.name == "Bullet(Clone)"){
var enemysLeft;
enemysLeft = 0;
for(var fooObj : GameObject in GameObject.FindGameObjectsWithTag("enemy")){
enemysLeft++; //for every enemy left we will increase a var
}
Destroy(col.gameObject);
enemysLeft--;
if(enemysLeft <= 0){ //We use <= because if it finds no enemies some how, decreasing 1 from 0 will result in -1.
Application.LoadLevel("menu");
Destroy(gameObject); //this probably won't ever get executed but unity will destroy it for us once loaded
}
}
This would not work. It only destroys the enemies if there are none left. So in other words, they will never get destroyed. You need to move both the destroys outside of the if(enemysLeft == 0) check.
Destroying the enemy and the bullet should be done regardless if there are any more or not :)
And you want to decrease the enemysLeft by 1 after doing the destroying and only then do the check. Because with this code, after destroying the last enemy, it will never load a level, because it would need to hit a new enemy to even enter this segment. But of course there would not be any left.
Ah you are absolutely correct, great catch.
I've never done thing like this but when I was looking at your code I was thinking that destroying the gameobject before loading the level would cause the level to not load.
I always assumed that once the gameobject is killed any other code is not executed. Basically the same way that if you load the level anything under that isn't called.
Am I wrong on this?
Huh... Yeah, that might be true also. Frankly the load new level bit should have a script of its own. Probably in an empty game object like "scene$$anonymous$$anager" or something.
Answer by Em3rgency · Jul 06, 2013 at 08:06 PM
function OnCollisionStay (col : Collision)
{
if(col.gameObject.name == "Bullet(Clone)")
{
Destroy(col.gameObject);
numberOfEnemiesLeft--;
if(numberOfEnemiesLeft == 0)
Application.LoadLevel("menu");
Destroy(gameObject);//This needs to be last, because if we destroy the gameObject before checking for the new level, that code might never get executed.
}
}
You either need to set how many enemies you have in your scene or you need to count them manually, maybe with finding them by tag or something...
thanks for the answer, since im new to unity i have to ask where do i put numberofenemiesleft variable?
Same place you put all the new variables. Right after class declaration, right before the start() function :)
Ok, so apparently we're ALL wrong. Edited my script slightly to fix the issue.
No, I decrease the count before the check, so == 0 works fine here.
Answer by Xentarok · Jul 06, 2013 at 08:13 PM
I am better at writing in C#, bu I think you get the ideea:
var NumberOfEnemies : int;
function OnCollisionStay (col : Collision)
{
if(col.gameObject.name == "Bullet(Clone)")
{
NumberOfEnemies--;
Destroy(col.gameObject);
Destroy(gameObject);
}
if(NumberOfEnemies == 0){
Application.LoadLevel("menu");
}
}
That would not work. You would get an error on line 20.
Not sure if it was on purpose or not but
if(NumberOfEnemies == 0){
Application.LoadLevel("menu");
}
Is not inside the collision method. In fact is isn't in ANY method which is why it would not work. Perhaps suggesting to put that in the update method or something somewhere similar would help more people.
Your answer
Follow this Question
Related Questions
Collsions problem 0 Answers
Same script, different properties to curtain objects 1 Answer
Decreasing Player Health On Collision with Enemy 2 Answers
How to access a bool of a specific clone in a collision 1 Answer
Collisions not working. 1 Answer