Change Text From Script Not Working

The error:

NullReferenceException: Object reference not set to an instance of an object
SlotMachine.Update () (at Assets/SlotMachine.cs:35)

The code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SlotMachine : MonoBehaviour
{

    public bool slotHandlePressed;
    public bool isSpinning;

    public int rowOneNum;
    public int rowTwoNum;
    public int rowThreeNum;

    public Text  rowOneText;
    bool changeText;
  

    void Start()
    {

        rowOneText = GetComponent<UnityEngine.UI.Text>();
      

    }



    void Update()
    {
        if (changeText == true)
        {
          
            rowOneText.text = "TEST";
        }
      
    }


    void OnMouseDown()
    {
        if (slotHandlePressed && isSpinning == false)
        {
            isSpinning = true;
          
            RandomNumberGenerator();
        }
    }

  

    void RandomNumberGenerator()
    {
        int rowOneNum = Random.Range(1, 9);
        int rowTwoNum = Random.Range(1, 9);
        int rowThreeNum = Random.Range(1, 9);


        changeText = true;

    }

  
}

When I place the code to change the text in the Start function it works fine however anywhere else and it does not work.

:frowning:

Are you assigning rowOneText from the inspector? If so, go ahead and delete line 23 and you’ll be good to go.

I never assigned it from the inspector and when I try and remove line 23 and assign it from inside the inspector it still returns the same error.