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)
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();
}
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);
}
}