Level 2 is not loading

I have made the first level of my game and when I complete what i have to to get in the next level but it stays at the first level. I tried to put the levels in the scenes in build section and saved it but it just did the same thing and stayed on level one. Can anyone help? Thank you!

Hi @ChipotleGod ,

You need to double-check that Level2, Level3 and Level4 are included in the Build settings: Unity - Manual: Build Settings

Good luck with it!

Yay! Someone responded! I did put my levels in the build settings and pressed save. I had 2 levels.I also tried to take them out and put them back in the build settings. So I am very confused, hope someone can help. Thank you!

@ChipotleGod

Seems like (in your error message) you have named your level “Level2” and “Level3” but the actual files have the numbers written as words… “LevelOne” and so on. So in your code you seem to be using wrong string values, no such levels will be found.

So what do I have to do to fix that?

“So what do I have to do to fix that?”

If your level file is named “levelOne”, shouldn’t you try to get it from code using exactly the same name, “levelOne”, instead of using something else, that doesn’t exist as a file (like “level1”) ?

Oh ok, I will try that as soon as I can and will get back to you! Thank you!

using UnityEngine;                                            
using UnityEngine.SceneManagement;

public class LevelController : MonoBehaviour
{
    private static int _nextLevelIndex = 1;
    private Enemy[] _enemies;

    private void OnEnable()
    {
        _enemies = FindObjectsOfType<Enemy>();
    }

    // Update is called once per frame
    void Update()
    {
       foreach(Enemy enemy in _enemies)
        {
            if (enemy != null)
                return;
        }

        Debug.Log("You killed all enemies");

        _nextLevelIndex++;
        string nextLevelName = "Level" + _nextLevelIndex;
        SceneManager.LoadScene(nextLevelName);
    }
}

Here is the code, where do i put the words LevelOne?

Where you assign “nextLevelName”.
Though with the way you have this set up by using an index value in the name of a scene, it would probably just be faster to rename your scenes from “LevelOne”, “LevelTwo”, “LevelThree” to “Level1”, “Level2”, “Level3” and so on.

OH MY GOD!!! Thank you so much!! This is my first time on unity forums actually posting! You were so helpful and it worked!! Thank you!

1 Like

@ChipotleGod

…That is what I already said too in my posts…

“Seems like (in your error message) you have named your level “Level2” and “Level3” but the actual files have the numbers written as words… “LevelOne” and so on. So in your code you seem to be using wrong string values, no such levels will be found.”

1 Like