I am trying to make a level select menu. Here is a screenshot of my layout:
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:


The error i get is:
NullReferenceException: Object reference not set to an instance of an object
ChooseLevel.Update () (at Assets/ChooseLevel.cs:28)
ChooseLevelobject?string lvlName. you never, ever, do this with strings. initialize it to a blank string by default.string lvlName = ""