Help with level selector

Hello

I am making a level selector that you can unlock levels on and when I try to unlock level 3, I get an error in the console. The error is:
NullReferenceException: Object reference not set to an instance of an object
GameManager.CompleteLevel () (at Assets/Scripts/GameManager.cs:19)
EndTrigger.OnTriggerEnter () (at Assets/Scripts/EndTrigger.cs:9)

If you need me to give you any code, just ask.

Thanks, Hayden

NullReferenceException means you didn’t set the value. look at GameManager.cs: line 19 to see what it’s complaining about

This is line 19

gameManager.WinLevel();

And this is WinLevel

public void WinLevel()
{
Debug.Log(“LEVEL WON”);
PlayerPrefs.SetInt(“levelReached”, levelToUnlock);
SceneManager.LoadScene(nextLevel);
}

post your gamemanger code and please use code tags - Using code tags properly - Unity Engine - Unity Discussions

we need more code.
what is nextLevel? a string?
SceneManager.LoadScene(nextLevel);

That means gameManager is null. Where do you set the value of gameManager? Post that code.

Ok. I’ll get the whole GameManager code later

GameManager script.

using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;

public class GameManager : MonoBehaviour {

bool gameHasEnded = false;
public float restartDelay = 1f;
public GameObject completeLevelUI;
public GameManager gameManager;
public string nextLevel = “Level02”;
public int levelToUnlock = 2;
public float completeDelay = 1f;

public void CompleteLevel()
{
completeLevelUI.SetActive(true);
gameManager.WinLevel();
}

public void EndGame ()
{
if (gameHasEnded == false)
{
gameHasEnded = true;
Debug.Log(“GAME OVER”);
Restart();
}

}
void Restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}

public void WinLevel()
{
Debug.Log(“LEVEL WON”);
PlayerPrefs.SetInt(“levelReached”, levelToUnlock);
SceneManager.LoadScene(nextLevel);
}

}

You don’t need thispublic GameManager gameManager;unless you want to make a static instance.

You can also directly call WinLevel

gameManager.WinLevel()

//Should be

WinLevel();
1 Like

I think WarmedxMints is correct.

using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;

public class GameManager : MonoBehaviour {

    bool gameHasEnded = false;
    public float restartDelay = 1f;
    public GameObject completeLevelUI;
    public GameManager gameManager; //not needed.  this is it's self
    public string nextLevel = "Level02";
    public int levelToUnlock = 2;
    public float completeDelay = 1f;

    public void CompleteLevel()
        {
            completeLevelUI.SetActive(true);
            WinLevel();
        }

    public void EndGame ()
    {
        if (gameHasEnded == false)
        {
            gameHasEnded = true;
            Debug.Log("GAME OVER");
            Restart();
        }
    }
   
    void Restart()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }


    public void WinLevel()
    {
        Debug.Log("LEVEL WON");
        PlayerPrefs.SetInt("levelReached", levelToUnlock);
        SceneManager.LoadScene(nextLevel);
    }

}

Ok. Thanks. I’ll try this later

Right. That stopped the error, but now when I play the game, it stays on one level.

using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;

public class GameManager : MonoBehaviour {

    bool gameHasEnded = false;
    public float restartDelay = 1f;
    public GameObject completeLevelUI;
    public string nextLevel = "Level02";
    public int levelToUnlock = 2;
    public float completeDelay = 1f;

    public void CompleteLevel()
    {
        completeLevelUI.SetActive(true);
        WinLevel();
    }



    public void EndGame ()
    {
        if (gameHasEnded == false)
        {
            gameHasEnded = true;
            Debug.Log("GAME OVER");
            Restart();
        }

      
    }
    void Restart()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }


    public void WinLevel()
    {
        Debug.Log("LEVEL WON");
        PlayerPrefs.SetInt("levelReached", levelToUnlock);
        SceneManager.LoadScene(nextLevel);
    }
  
}

Is Level02 included in your build?

As in, is it in the project folder? Yes

I think he meant in the project build settings page

it needs to be in the project build settings, if it is’t added then unity should have given an error when you tried to load it.

I’m sure that all the levels are in the build settings but I’ll check later. And I agree with johne5.

Is your situation like this: You play level 1, unlock level 2 and then it reloads level 1?

No, not exactly. My situation is that when I play level 1 it unlocks two, then when I win level 2 it goes back to the start of level 2.