Search code examples
c#unity-game-enginecounter2d-games

Unity3D Shooter - How to switch to next level after all enemies get destroyed?


I am a newbie to Unity and I am trying to build a little shooter 2d game in C#. I am now stuck and confess that I am a little lost not knowing what the best approach is, but the problem is that my hero shoots at the enemies and they die but how do I get to the next level after the enemies are all dead? If I make a dead counter, what script do I put in? In the enemy script? Or do I make a new script but associate it with what? I also need the game to end if the hero fires his six bullets (already have a counter that makes the hero not shoot anymore after six shoots) and there are still enemies left ... Does anyone give me some tips? Thanks!

Enemy script:

  using System.Collections.Generic;
  using UnityEngine;

  public class BadguyScript : MonoBehaviour
  {
      public int maxHealth;
      public int curHealth;
      private Animator myAnimator;
      private bool isDead;
      [SerializeField]
      private float DespawnTime = 2.5f;
      [SerializeField]
      private string DeathAnimHash = "isDead"; 

      void Start()
      {
          myAnimator = GetComponent<Animator>();
          myAnimator.enabled =true;
          myAnimator.SetBool (DeathAnimHash ,isDead);


          maxHealth = 1;
          curHealth = maxHealth;

      }
      void Update()
      {
          if (curHealth < 1)
          {
              isDead = true;
              myAnimator.SetBool (DeathAnimHash ,isDead);
              Destroy(gameObject,DespawnTime);
          }
      }
      void OnTriggerEnter2D(Collider2D col)
      {
          if (isDead)
             return;
          if (col.tag == "bullet")
          {
              curHealth -= 1;
              Destroy(col.gameObject);
          }
      }
  }

Count Bullets Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameFlow : MonoBehaviour
{

    public static float remainingShots = 6;
    public Transform shot1;
    public Transform shot2;
    public Transform shot3;
    public Transform shot4;
    public Transform shot5;
    public Transform shot6;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
      if (remainingShots > 0)
      {
        shot1.GetComponent<Image> ().enabled = true;
      }
      else
      {
        shot1.GetComponent<Image> ().enabled = false;
      }

      if (remainingShots > 1)
      {
        shot2.GetComponent<Image> ().enabled = true;
      }
      else
      {
        shot2.GetComponent<Image> ().enabled = false;
      }

      if (remainingShots > 2)
      {
        shot3.GetComponent<Image> ().enabled = true;
      }
      else
      {
        shot3.GetComponent<Image> ().enabled = false;
      }

      if (remainingShots > 3)
      {
        shot4.GetComponent<Image> ().enabled = true;
      }
      else
      {
        shot4.GetComponent<Image> ().enabled = false;
      }

      if (remainingShots > 4)
      {
        shot5.GetComponent<Image> ().enabled = true;
      }
      else
      {
        shot5.GetComponent<Image> ().enabled = false;
      }

      if (remainingShots > 5)
      {
        shot6.GetComponent<Image> ().enabled = true;
      }
      else
      {
        shot6.GetComponent<Image> ().enabled = false;
      }


      if(Input.GetButtonDown("Fire1"))
      {
      remainingShots -= 1;
      }
    }
}

Solution

  • To switch to another scene after your conditions do the following:
    1. Add the OtherScenes to your game by doing this:

    File -> Build Settings -> Add Open Scenes


    2. Do something like this in your code:

    Enemy Script.cs
    
    using UnityEngine.SceneManagement; // Contains scene management functions
    
    public GameObject[] enemies;
    
    void Update()
    {
        enemies = GameObject.FindGameObjectsWithTag("Enemy"); // Checks if enemies are available with tag "Enemy". Note that you should set this to your enemies in the inspector.
        if (enemies.length == 0)
        {
            SceneManager.LoadScene("OtherSceneName"); // Load the scene with name "OtherSceneName"
        }
    }
    
    Bullet Script.cs
    using UnityEngine.SceneManagement;
    
    void Update()
    {
        if (remainingShots == -1)
        {
            SceneManager.LoadScene("OtherSceneName");
        }
    }