'I want to change scene when the counter reaches 0 but it does not work

public class EnemyCounter : MonoBehaviour
{
    GameObject[] enemies;

    public Text enemyCountText;

    void Start()
    {
    }

    void Update()
    {
        enemies = GameObject.FindGameObjectsWithTag("bad");

        enemyCountText.text = "Enemies : " +
                              enemies.Length.ToString();

        if (enemies == null)
        {
            SceneManager.LoadScene("Level2");
        }
    }
}


Solution 1:[1]

This code does not work when you have defined a list. In fact, a list with zero members is not equal to null. null means no list definition.

enemies == null;

Change the code this way:

if (enemies.Length == 0)
{
    SceneManager.LoadScene("Level2");
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 KiynL