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 NEWBIE_1 · Jun 26, 2015 at 07:56 AM · load level

Loading Level when last enemy dies.

I am new to programming as i am an artist. I am trying to create a C# that will load a new scene/level when the last enemy dies. I dont know where to start... My enemies are prefabs and get spawned in a certain amount after a certain time. i have tags for each player "Enemy1" "Enemy2" "Enemy3". Please and thank you!!

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Hellium · Jun 26, 2015 at 08:25 AM

You could use something like this :

 using UnityEngine;
 using System.Collections;
 
 public class Enemy : MonoBehaviour
 {
     // Number of enemies alive
     private static int aliveCounter = 0 ;
 
     void Start()
     {
         aliveCounter++ ;
     }
     
     void OnKilled()
     {
         aliveCounter-- ;
         
         if( aliveCounter == 0 )
             Application.LoadLevel("NextLevel") ;
     }
 
 }


Where the OnKilled function is obviously called when your enemy is killed.

The aliveCounter attribute is a static integer, common to all instances of the class Enemy. All your enemies will share the value of this attribute.


Note to @robbagus 's code : This code must be held by a GameController while the solution I proposed must be attached to an enemy directly.

Though, I think that the "death check" isn't well thought. The enemy who has just died should call a function of the GameController. The same way, another function will be necessary to warn the GameController a new Enemy has appeared :

 //C#
 
 using UnityEngine;
 using System.Collections;
 
 public class GameController : MonoBehaviour
 {
 
     private int enemyLeft = 0 ;

     public void EnemyHasDied()
     {
         enemyLeft--;
      
         if (enemyLeft == 0)
         {
             // do your thing here, loading scene etc
         }
     }


     public void EnemyHasAppeared()
     {
         enemyLeft++;
     }
 }


Comment
Add comment · Show 10 · 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 Hellium · Jun 26, 2015 at 01:33 PM 0
Share

@NEWBIE_1 : Take a look at the solution I proposed :

Attach the last script to an empty GameObject called GameController.

When an ennemy is instantiated, call the function EnemyHasAppeared() of this script When an enemy is killed, call the function EnemyHasDied() of the same script

Don't forget to add Application.LoadLevel("LevelName"); ins$$anonymous$$d of the comments of the EnemyHasDied() function.

avatar image NEWBIE_1 · Jun 26, 2015 at 01:46 PM 0
Share

i have made this so far, but how will the script know what objects are my enemies? I saw one person say if agemobject with tag "Enemy" is less than 0 Application.loadlevel("LevelName"); Is this something that is possible? Sorry im an artist just trying to make a simple game.

using UnityEngine; using System.Collections;

public class GameController : $$anonymous$$onoBehaviour { private int enemyLeft = 0;

 public void EnemyHasDied()
 {
     enemyLeft--;

     if (enemyLeft == 0) 
     {
         Application.LoadLevel("Level_01");
     }
 }

 public void EnemyHasAppeared()
 {
     enemyLeft++;
 }

}

avatar image Hellium · Jun 26, 2015 at 01:57 PM 0
Share

The GameController won't know it, the enemies themself will warn the GameController.

Do your enemies have a script on them ? If so :

Next to the attributes add :

 public GameController GameController ;

Then, drag and drop the GameController object into the field inside the inspector


In their Start function, add :

 GameController.EnemyHasAppeared()



In the function telling them to die (with the Destroy function is suppose), add :

 GameController.EnemyHasDied()



Have you done the tutorials before trying to create your own game ?

avatar image NEWBIE_1 · Jun 26, 2015 at 02:06 PM 0
Share

ive watched the tutoial on making a survival game by unity projects. i am just getting really confused with all of this :(

avatar image Hellium · Jun 26, 2015 at 02:10 PM 0
Share

The PROJECT: SURVIVAL SHOOTER ?

Have you watched the lessons in this page (below the projects) ?

https://unity3d.com/learn/tutorials/modules

I would advise you to watch the scripting videos if you plan to do logic stuff in your game. Don't try to script anything if you have never made programs before.

Show more comments
avatar image
0

Answer by robbagus · Jun 26, 2015 at 08:28 AM

The most straight forward solution is to have an integer counter checking the amount of enemies left.

 int enemyLeft = 5; // sample number
 
 if (enemyDie) // do your enemy death check here
 {
     enemyLeft--;
 }
 
 if (enemyLeft == 0)
 {
    // do your thing here, loading scene etc
 }

Although, I'm a bit confused why would you have different tags for your enemies since they are all the same (prefab). Are you giving each enemy different behavior later on?

Comment
Add comment · Show 1 · 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 NEWBIE_1 · Jun 26, 2015 at 01:29 PM 0
Share

I am really confused could someone write a straigh forward script that will allow me to accomplish the goal im trying to get?

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

6 People are following this question.

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

Related Questions

How to Eliminate Monitor Artifacts When Loading Scene? 1 Answer

NullReferenceException and m_InstanceID == 0 on LoadLevel (C#) 1 Answer

How to make a script that changes level when you press enter at a door? 1 Answer

Log Callback after Scene change 1 Answer

Como fazer download de arquivos enquanto o jogo funciona 0 Answers


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