# Help with level selector

**URL:** https://discussions.unity.com/t/help-with-level-selector/664139
**Category:** Scripting
**Tags:** Scripting
**Created:** [May 9, 2017, 4:03pm UTC](https://discussions.unity.com/t/help-with-level-selector/664139 "2017-05-09T16:03:16Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![haydenbrown39](https://avatars.discourse-cdn.com/v4/letter/h/e19b73/32.png) [@haydenbrown39](https://discussions.unity.com/u/haydenbrown39)
#### Post date: [May 9, 2017, 4:03pm UTC](https://discussions.unity.com/t/help-with-level-selector/664139/1 "2017-05-09T16:03:16Z")

</div>

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

---

<div class="post-metadata">

### Author: ![johne5](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/johne5/32/476635_2.png) [@johne5](https://discussions.unity.com/u/johne5)
#### Post date: [May 9, 2017, 4:05pm UTC](https://discussions.unity.com/t/help-with-level-selector/664139/2 "2017-05-09T16:05:32Z")

</div>

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

---

<div class="post-metadata">

### Author: ![haydenbrown39](https://avatars.discourse-cdn.com/v4/letter/h/e19b73/32.png) [@haydenbrown39](https://discussions.unity.com/u/haydenbrown39)
#### Post date: [May 10, 2017, 3:49pm UTC](https://discussions.unity.com/t/help-with-level-selector/664139/3 "2017-05-10T15:49:41Z")

</div>

This is line 19

gameManager.WinLevel();

---

<div class="post-metadata">

### Author: ![haydenbrown39](https://avatars.discourse-cdn.com/v4/letter/h/e19b73/32.png) [@haydenbrown39](https://discussions.unity.com/u/haydenbrown39)
#### Post date: [May 10, 2017, 3:50pm UTC](https://discussions.unity.com/t/help-with-level-selector/664139/4 "2017-05-10T15:50:16Z")

</div>

And this is WinLevel

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

---

<div class="post-metadata">

### Author: ![WarmedxMints\_1](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/warmedxmints_1/32/554395_2.png) [@WarmedxMints\_1](https://discussions.unity.com/u/WarmedxMints_1)
#### Post date: [May 10, 2017, 4:41pm UTC](https://discussions.unity.com/t/help-with-level-selector/664139/5 "2017-05-10T16:41:02Z")

</div>

post your gamemanger code and please use code tags - [Using code tags properly - Unity Engine - Unity Discussions](https://forum.unity3d.com/threads/using-code-tags-properly.143875/)

---

<div class="post-metadata">

### Author: ![johne5](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/johne5/32/476635_2.png) [@johne5](https://discussions.unity.com/u/johne5)
#### Post date: [May 10, 2017, 4:48pm UTC](https://discussions.unity.com/t/help-with-level-selector/664139/6 "2017-05-10T16:48:04Z")

</div>

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

---

<div class="post-metadata">

### Author: ![StarManta](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/starmanta/32/280327_2.png) [@StarManta](https://discussions.unity.com/u/StarManta)
#### Post date: [May 10, 2017, 5:15pm UTC](https://discussions.unity.com/t/help-with-level-selector/664139/7 "2017-05-10T17:15:39Z")

</div>

> [@haydenbrown39](#):
>
> This is line 19
> 
> gameManager.WinLevel();

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

---

<div class="post-metadata">

### Author: ![haydenbrown39](https://avatars.discourse-cdn.com/v4/letter/h/e19b73/32.png) [@haydenbrown39](https://discussions.unity.com/u/haydenbrown39)
#### Post date: [May 10, 2017, 6:09pm UTC](https://discussions.unity.com/t/help-with-level-selector/664139/8 "2017-05-10T18:09:06Z")

</div>

Ok. I’ll get the whole GameManager code later

---

<div class="post-metadata">

### Author: ![haydenbrown39](https://avatars.discourse-cdn.com/v4/letter/h/e19b73/32.png) [@haydenbrown39](https://discussions.unity.com/u/haydenbrown39)
#### Post date: [May 10, 2017, 6:37pm UTC](https://discussions.unity.com/t/help-with-level-selector/664139/9 "2017-05-10T18:37:04Z")

</div>

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

}

---

<div class="post-metadata">

### Author: ![WarmedxMints\_1](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/warmedxmints_1/32/554395_2.png) [@WarmedxMints\_1](https://discussions.unity.com/u/WarmedxMints_1)
#### Post date: [May 10, 2017, 7:04pm UTC](https://discussions.unity.com/t/help-with-level-selector/664139/10 "2017-05-10T19:04:18Z")

</div>

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

You can also directly call WinLevel

```csharp
gameManager.WinLevel()

//Should be

WinLevel();

```

---

<div class="post-metadata">

### Author: ![johne5](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/johne5/32/476635_2.png) [@johne5](https://discussions.unity.com/u/johne5)
#### Post date: [May 10, 2017, 9:40pm UTC](https://discussions.unity.com/t/help-with-level-selector/664139/11 "2017-05-10T21:40:05Z")

</div>

I think WarmedxMints is correct.

```csharp
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);
    }

}

```

---

<div class="post-metadata">

### Author: ![haydenbrown39](https://avatars.discourse-cdn.com/v4/letter/h/e19b73/32.png) [@haydenbrown39](https://discussions.unity.com/u/haydenbrown39)
#### Post date: [May 11, 2017, 7:32am UTC](https://discussions.unity.com/t/help-with-level-selector/664139/12 "2017-05-11T07:32:18Z")

</div>

Ok. Thanks. I’ll try this later

---

<div class="post-metadata">

### Author: ![haydenbrown39](https://avatars.discourse-cdn.com/v4/letter/h/e19b73/32.png) [@haydenbrown39](https://discussions.unity.com/u/haydenbrown39)
#### Post date: [May 11, 2017, 2:41pm UTC](https://discussions.unity.com/t/help-with-level-selector/664139/13 "2017-05-11T14:41:44Z")

</div>

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

```auto
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);
    }
  
}

```

---

<div class="post-metadata">

### Author: ![WarmedxMints\_1](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/warmedxmints_1/32/554395_2.png) [@WarmedxMints\_1](https://discussions.unity.com/u/WarmedxMints_1)
#### Post date: [May 11, 2017, 2:44pm UTC](https://discussions.unity.com/t/help-with-level-selector/664139/14 "2017-05-11T14:44:58Z")

</div>

Is Level02 included in your build?

---

<div class="post-metadata">

### Author: ![haydenbrown39](https://avatars.discourse-cdn.com/v4/letter/h/e19b73/32.png) [@haydenbrown39](https://discussions.unity.com/u/haydenbrown39)
#### Post date: [May 11, 2017, 4:07pm UTC](https://discussions.unity.com/t/help-with-level-selector/664139/15 "2017-05-11T16:07:47Z")

</div>

As in, is it in the project folder? Yes

---

<div class="post-metadata">

### Author: ![methos5k](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/methos5k/32/387158_2.png) [@methos5k](https://discussions.unity.com/u/methos5k)
#### Post date: [May 11, 2017, 4:08pm UTC](https://discussions.unity.com/t/help-with-level-selector/664139/16 "2017-05-11T16:08:49Z")

</div>

I think he meant in the project build settings page

---

<div class="post-metadata">

### Author: ![johne5](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/johne5/32/476635_2.png) [@johne5](https://discussions.unity.com/u/johne5)
#### Post date: [May 11, 2017, 4:58pm UTC](https://discussions.unity.com/t/help-with-level-selector/664139/17 "2017-05-11T16:58:03Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![haydenbrown39](https://avatars.discourse-cdn.com/v4/letter/h/e19b73/32.png) [@haydenbrown39](https://discussions.unity.com/u/haydenbrown39)
#### Post date: [May 11, 2017, 4:59pm UTC](https://discussions.unity.com/t/help-with-level-selector/664139/18 "2017-05-11T16:59:22Z")

</div>

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

---

<div class="post-metadata">

### Author: ![methos5k](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/methos5k/32/387158_2.png) [@methos5k](https://discussions.unity.com/u/methos5k)
#### Post date: [May 11, 2017, 5:08pm UTC](https://discussions.unity.com/t/help-with-level-selector/664139/19 "2017-05-11T17:08:39Z")

</div>

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

---

<div class="post-metadata">

### Author: ![haydenbrown39](https://avatars.discourse-cdn.com/v4/letter/h/e19b73/32.png) [@haydenbrown39](https://discussions.unity.com/u/haydenbrown39)
#### Post date: [May 11, 2017, 8:42pm UTC](https://discussions.unity.com/t/help-with-level-selector/664139/20 "2017-05-11T20:42:01Z")

</div>

> [@methos5k](#):
>
> 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.

[Next page](https://discussions.unity.com/t/help-with-level-selector/664139.md?page=2)
