.toString conversion leads to 1 or more Null reference exceptions.

Hello,

I’m trying to use a .toString(); to convert an int to string and display it in a text field in my canvas, text box. My placement of it seems to either lead to only a single null reference exception or 3 +. I’ve tried moving this .toString() into from my Start(), to my StartGame() method but each of these just adds more null reference exceptions. The text in this text box needs to display once the scene is loaded in. I have a .toString() convert in my nextGuess() method and that works fine once someone clicks one of the required buttons, but having another before seems to break this? Any ideas or articles that could help would be appericated.

public class GameLogic : MonoBehaviour
{
    [SerializeField] int initialLowerBound, initialUpperBound;
    [SerializeField] private Text textgo;
    GameObject gameob;
    int guess ;
    Text randomNumber;


    public void Start()
    {
        randomNumber = textgo;
        StartGame();
       //text.go = guess.toString();            Exception if placed here 
      
    }






    public void StartGame()
    {

        guess = Random.Range(1, 1000);
        initialLowerBound = 1; initialUpperBound = 1000;
        //textgo.text = guess.ToString();           Exception if placed  here 
        Debug.Log("text handle is" + textgo);
        Debug.Log("Start Game Was Invoked" + transform.name);
      
    }


    public void GuessHigher()
    {

        initialLowerBound = guess - 1;
        nextGuess();

        Debug.Log("Higher Button Pressed, " + guess);
    }

    public void GuessLower()
    {

        initialUpperBound = guess + 1;
        nextGuess();

        Debug.Log("Lower button Pressed" + guess);
    }


     void nextGuess()
    {
        guess = Random.Range(initialLowerBound, initialUpperBound + 1);
        textgo.text = guess.ToString();           // Actually works with UI text Box
    }







}

Nothing special about .ToString()… this is just a bog standard yawn boring nullref.

The answer is ALWAYS the same for those… ALWAYS

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null
  • Identify why it is null
  • Fix that