0

I am trying to make a level select menu. Here is a screenshot of my layout: enter image description here To make this work, this is my script for making it happen:

    using UnityEngine;
using UnityEngine.UI;

public class ChooseLevel : MonoBehaviour
{

    string[] levelNames;
    int i = 0;


    public Button addIndex;
    public Button subtractIndex;
    public Text levelChooser;

    string lvlName = "";

    void Start()
    {
        levelNames = PlayMenu.levelNames;

        addIndex.onClick.AddListener(delegate () { i += 1; });
        subtractIndex.onClick.AddListener(delegate () { i -= 1; });

    }

    void Update()
    {
        i = Mathf.Clamp(i, 0, levelNames.Length);
        lvlName = levelNames[i];
        levelChooser.text = lvlName;
    }

}

The code is attached to the canvas and here are screen shots of the scene and a section of the inspector for the canvas:
enter image description here
enter image description here

The error i get is:

NullReferenceException: Object reference not set to an instance of an object
ChooseLevel.Update () (at Assets/ChooseLevel.cs:28)
5
  • Do you ever create a ChooseLevel object? Commented Apr 12, 2016 at 16:16
  • 1
    you should never, ever, ever use 'SerializeField" for any reason. simply mark the field "public". it's that simple. that's if you're absolutely sure you want that behavior Commented Apr 12, 2016 at 16:22
  • 1
    this code "Mathf.Clamp(i, 0, levelNames.Length);" is meaningless. you probably meant "i = Mathf.Clamp(i, 0, levelNames.Length);" Commented Apr 12, 2016 at 16:23
  • note that the pattern is often "i = i % length" to make it wrap (but, take care with negatives) Commented Apr 12, 2016 at 16:24
  • note that you have string lvlName. you never, ever, do this with strings. initialize it to a blank string by default. string lvlName = "" Commented Apr 12, 2016 at 16:27

2 Answers 2

2

You declare your string right above void Start(), but don't initialize it. (Implicitly null)

string lvlName;

So then when you go in Update() for the first time, the value of lvlName is null.

void Update()
{
    levelChooser.text = lvlName; //null the first time 
    lvlName = levelNames[i]; //THEN it's changed
    Mathf.Clamp(i, 0, levelNames.Length);
}

So depending on your game logic you must either FIRST assign to lvlName or initialize it with an empty string. I guess

void Update()
{
    i = Mathf.Clamp(i, 0, levelNames.Length); //FIRST clamp. Thanks to Joe Blow for pointing out that it never gets reassigned.
    lvlName = levelNames[i]; //then update lvlName
    levelChooser.text = lvlName; //then Change the text. 
}

should work fine. Also make sure that i doesn't hit the exact value levelNames.Length but I think that logic is okay. You'll notice when you get an out-of-bounds exception.

Sign up to request clarification or add additional context in comments.

Comments

0

It was just a small error in another script.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.