[Solved] Null Reference Exception on Instantiating text in a Canvas

I can’t see what I’m doing wrong.

I want to spawn text into the canvas as a child, but regardless of what I try I get a Null Reference exception. However, if I run debug.Log on canvas it brings back the object with the proper reference.

//canvas is attached in the inspector
public Canvas canvas;

void Update(){
   //scoreFloat is the text
   Text tempTextBox = Instantiate(scoreFloat, objectPos, Quaternion.identity) as Text;
   //Debug.Log(canvas.name); - returns correct reference
    tempTextBox.transform.SetParent(canvas.transform, false); //throws out null-reference exception
}

Anyone got any ideas?

I just want to spawn the scoreFloat as a child of the Canvas.

Well Instantiate returns an object, which is actually the GameObject component of the instantiated prefab. That is why you have to write " as GameObject " when you Instantiate. What you are trying to do Is convert GameObject To Text (by writing as Text), like converting int to float, which does not work, so c# returns null. You want to get the Text component using .GetComponent

Thanks for explaining.

I worked it out.

Final code.

//canvas is attached in the inspector
public GameObject canvas;


void Update(){
   //scoreFloat is the text
   GameObject tempTextBox = Instantiate(scoreFloat, objectPos, Quaternion.identity) as GameObject;
   scoreFloat.GetComponent<Text>().text = "10";

    tempTextBox.transform.SetParent(canvas.transform, false);
}

No, no, no, there is a mistake in your code :). don’t take the Text component from the prefab (scoreFloat), take it from your Instantiated object (tempTextBox). Prefabs should be use only for Instantiate and nothing more, as it will brake your prefabs, and save all the changes you do to it

Seems to be working fine. I’m dynamically updating the text component of the prefab with scores. And the prefabs get destroyed in about a second.

    //UI
    public Text scoreText;
    public int score;
    public GameObject canvas;

void Start(){
        //score assignment
        score = 0;
        UpdateScore();
}

void Update(){
                        int pointWeight = 10;
                        AddScore(pointWeight);
                        GameObject tempTextBox = Instantiate(scoreFloat, objectPos, Quaternion.identity) as GameObject;
                        scoreFloat.GetComponent<Text>().text = pointWeight.ToString();
                        tempTextBox.transform.SetParent(canvas.transform, false);
                        tempTextBox.GetComponent<RectTransform>().position = objectPos;
                        totalDuration = totalDuration - 0.01f;
                        hasDinged = true;
}

    void UpdateScore()
    {
        scoreText.text = "Score: " + score;
    }

    public void AddScore (int newScoreValue)
    {
        score += newScoreValue;
        UpdateScore();
    }

Ah, I see, I thought you were trying to change tempTextBox text. I am not sure who scoreFloat is (I work a bit in a different way with my prefabs), and what are you trying to do, but please note that Instantiate on Update is a bad idea, and is usually very slow. But if you succeeded to accomplish your effect i’m happy. If you are curious on why Instantiate on update is a bad idea, and how should you solve your problem instead, maybe you should make another post, with some details about your situation.

I realise there can be performance issues on instantiating on update. I didn’t add in the code on my replies, but the instantiate is only executed on touch.

Thanks for your help.