Wes
Wes

Reputation: 33

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 
{   
    void OnTriggerEnter(Collider collider)
    {
        if (collider.gameObject.tag == "Player") 
        {
            Destroy(gameObject);
        }
    } 
}

Any help is appreciated.

Upvotes: 2

Views: 796

Answers (1)

Hossein Rashno
Hossein Rashno

Reputation: 3469

  1. Create a tag for spheres like Enemy or what ever you want.
  2. In BoxDestroy class, Before you destroy the object, calculate the count of remaining objects and if its equal to one then load another scene:

    using UnityEngine;
    using System.Collections;
    
    public class BoxDestroy : MonoBehaviour 
    {   
        void OnTriggerEnter(Collider collider)
        {
            if (collider.gameObject.tag == "Player") 
            {
    
                GameObject[] remainingObj = GameObject.FindGameObjectsWithTag("Enemy");
                if (remainingObj.Length == 1)
                {
                    Application.LoadLevel("name of level you want to load");
                }
    
                Destroy(gameObject);
            }
        } 
    }
    

Upvotes: 1

Related Questions