Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by wesnjai11 · Nov 18, 2015 at 05:48 AM · c#clonedestroy objectcounternext level

How can you load next the level after you destroy the last clone c#

I am almost complete with my game but I am just stuck on one part. I have to implement some statement that says when the last clone is destroyed the next level is loaded. Except I do not know how to do that. I have a respawner which makes like 20 clones of a sphere and when I collide with them they disappear. After 20 clones have been destroyed I want to advance to the next level. Can anyone help me out?.

Here's my respawner:

  using UnityEngine;
  using System.Collections;

  public class spawner : MonoBehaviour 
  {
  public GameObject objectToSpawn;
  public int numberOfEnemies;
  private float spawnRadius = 5;
  private Vector3 spawnPosition;
 // Use this for initialization

 void Start ()
     {
     SpawnObject();  
     }
 
     void Update () {}

     void SpawnObject() 
     {
     for (int i= 0; i < numberOfEnemies; i++)  
     { 
         spawnPosition = transform.position + Random.insideUnitSphere * spawnRadius; 
         Instantiate(objectToSpawn, spawnPosition, Quaternion.identity);
     }
    }
   }

Here's my BoxDestroy:

  using UnityEngine;
  using System.Collections;

  public class BoxDestroy : MonoBehaviour {
 public int x;
 
 void Start ()
     {
     x=10;
 
     }
 
 
 void OnTriggerEnter(Collider collider)
    {
     if (collider.gameObject.tag == "Player") 
     {    x--;
         Destroy(gameObject);


     }
     if(x==0){
         Application.LoadLevel(0);
     
      }
     
        }
 
 
 
 

} Any help is appreciated! :)

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ZefanS · Nov 18, 2015 at 06:20 AM

If you tag your enemies with a tag such as "Enemy", then you could do something like this in Update():

 if (GameObject.FindWithTag("Enemy") == null)
 {
     Application.LoadLevel("NextLevel");
 }

Or you could keep a count of how many enemies are left in the Scene and do something like:

 if (enemyCount <= 0)
 {
     Application.LoadLevel("NextLevel");
 }

Neither of these solutions are very elegant because the check gets called unnecessarily every frame. Alternatively, you could do something a bit smarter, and call a method to do a check like this only when you destroy a clone. Something like:

 public void CheckForEnemiesLeft()
 {
     if (GameObject.FindWithTag("Enemy") == null)
     {
         //Do any finishing up
         Application.LoadLevel("NextLevel");
     }
 }

This all assumes that you know which level you want to load next. For this I would use a level management GameObject that would keep track of which level should be loaded next. You would create this in the first scene and then call DontDestroyOnLoad() so that it persists throughout all the scenes.

The CheckForEnemiesLeft() function could be part of the level management script.

Hope this helps.

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

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

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

Destroy() not removing the object all the time (Coroutine used) 0 Answers

How to correctly count number of objects? 2 Answers

How can I change this script so that each instantiated prefab spawns a set amount faster 0 Answers

Unity3D: Objects destroy automatically 0 Answers

Adding a unique element from one list to another list 2 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