NullReferenceException: Object reference not set to an instance of an object

I’ve been getting this error: NullReferenceException: Object reference not set to an instance of an object
ChoiceButton.Start () (at Assets/ChoiceButton.cs:20)
and I have no clue what to do now. I’ve looked at other posts like this one but can’t figure it out.

public class ChoiceButton : MonoBehaviour {

    public Button button;
    public Text choiceText;

    private Choice choice;
    private ChoiceList choiceList;

    //[SerializeField] public bool currentBool;

    // Use this for initialization
    void Start () {
        button.onClick.AddListener(HandleClick);
	}

    public void SetUp(Choice currentChoice, ChoiceList currentChoiceList)
    {
        choice = currentChoice;
        choiceText.text = choice.choiceName;

        choiceList = currentChoiceList;
    }
    /*public void LoadStartScene()
    {
        SceneManager.LoadScene(0);
    }*/

    public void HandleClick()
    {
        choiceList.QuizButtonFunction(choice);
    }
}

Can anyone help? I’d really appreciate it, thanks.

Looking at your code, your SetUp method is never called, you are trying to access choiceList in the HandleClick method, which has been defined as the event handler for the onClick in the Start method, but this has not been instantiated.


See also;

Privacy & Terms