Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by erda123 · Jul 06, 2013 at 08:00 PM · javascriptcollisionenemyloadlevelenemies

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?

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image erda123 · Jul 06, 2013 at 08:35 PM 0
Share

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) ?

avatar image Em3rgency · Jul 06, 2013 at 08:38 PM 0
Share

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.

avatar image trs9556 · Jul 06, 2013 at 08:39 PM 0
Share

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.

avatar image erda123 · Jul 06, 2013 at 08:51 PM 0
Share

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?

avatar image Em3rgency · Jul 06, 2013 at 08:54 PM 0
Share

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 :)

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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
     }

 
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Em3rgency · Jul 06, 2013 at 08:18 PM 0
Share

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.

avatar image trs9556 · Jul 06, 2013 at 08:21 PM 0
Share

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?

avatar image Em3rgency · Jul 06, 2013 at 08:26 PM 0
Share

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.

avatar image
0

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...

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image erda123 · Jul 06, 2013 at 08:12 PM 0
Share

thanks for the answer, since im new to unity i have to ask where do i put numberofenemiesleft variable?

avatar image Em3rgency · Jul 06, 2013 at 08:22 PM 0
Share

Same place you put all the new variables. Right after class declaration, right before the start() function :)

avatar image Em3rgency · Jul 06, 2013 at 08:27 PM 0
Share

Ok, so apparently we're ALL wrong. Edited my script slightly to fix the issue.

avatar image Em3rgency · Jul 06, 2013 at 08:34 PM 0
Share

No, I decrease the count before the check, so == 0 works fine here.

avatar image
0

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");
     
     }
 
         }
     
 
 
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image trs9556 · Jul 06, 2013 at 08:20 PM 0
Share

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.

avatar image Xentarok · Jul 06, 2013 at 08:22 PM 0
Share

Yeah, it wasn't on purpose, my mistake, fixing it now.

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges