Change scene by killing 2 or more enemies

Hello, I’m making my first game, but I got stuck on this problem:
In my first level I have an enemy, and when I kill him if he passes level 2, the problem is my script, it is this:
Enemy Script:

    public void Hit()
    {
        Health = Health -1;
        if (Health == 0){
             Destroy(gameObject);
             SceneManager.LoadScene(2);
        }
    }

In the script of the enemy I put that when I die I advance in level, but of course, if the enemy is 1 it is fine, but when there are 2 or more, how can I do it?

a little help? I already searched and I didn’t find anything, or it doesn’t work for me, I know that this question is repeated, but go ahead.

So if you are going to consistently add more enemies each level and make it an increasing requirement to go to the next level, you should look into some sort of enemy controller. You dont want to put the Scene Loading on the actual enemy script, but on a controller script (on an empty gameobject). You can then have a scene manager script as well that has scene changing functions that get called by the enemy controller.

What the enemy controller does is determine the number of enemies in the scene, and then dynamically determines that X number of enemies are required to be defeated to advance to the next level.

So the logic follows something like this:
Start scene > EnemyController Counts # of enemies at Start > Player kills X enemies > EnemyController calls LoadNextScene function from SceneController > Load Next Scene > Rinse and Repeat

The Scene and Enemy controller scripts would be static objects that persist across scenes. Look up DontDestroyOnLoad or Singletons on Youtube.

1 Like