I'm trying to set the color property of a TMP_Text object but keep getting Object reference not set to instance of an object error. Setup is as follows;

  1. I have a Canvas (game hud) that has 6 TMP_Text objects in it for stats. 3 are static text and 3 are to be updated from other scripts.

  2. I have a gameobject that will be moving and a script that will change the text color of one of the values in the hud based on the game object velocity exceeding a fixed limit.

    using System.Collections.Generic;
    using UnityEngine;
    using TMPro;
    public class shipCollision : MonoBehaviour {
      // The TMP_Text object is dragged into the inspector before runtime
      public TMP_Text txtVertVelVal;
       private void Update() {
         landerRb = lander.GetComponent<Rigidbody>();
         Vector3 landerVelocity = landerRb.velocity;
         if(landerVelocity.y > 3f) {
           txtVertVelVal.color = Color.red;
         } else {
           txtVertVelVal.color = Color.green;

So, is this failing to because I am not referencing the parent Canvas maybe?

Update: Here's an image of my lander's inspector. image of my lander's inspector

  • \$\begingroup\$ View the gameObject with shipCollision in the inspector and verify txtVertVelVal has been assigned something. Also verify lander has a rigdbody. \$\endgroup\$
    – TTTTTTa
    Commented May 1 at 6:49
  • \$\begingroup\$ If you double click the object reference error, it should take you to the line with the error. Which line is the error pointing to? \$\endgroup\$
    – Paul
    Commented May 1 at 7:25
  • \$\begingroup\$ That error tells you that you have not set the reference. Since you do not set it in your script in start, you have to do it in the editor. If you set in the editor, you might have placed that script twice and one has not set a reference \$\endgroup\$
    – Zibelas
    Commented May 1 at 13:25
  • \$\begingroup\$ @TTTTTTa - I have assigned the TMP_Text object in the inspector already. However, the script acts like it's still null. Very strange as I expected that would be a sure fire reference to the text object. \$\endgroup\$
    – Skittles
    Commented May 2 at 1:37
  • \$\begingroup\$ @Paul - I have done that also and it takes me to this line; txtVertVelVal.color = Color.green; When I set a breakpoint and try viewing the value of txtVertVelVal, it equals null. \$\endgroup\$
    – Skittles
    Commented May 2 at 1:37

1 Answer 1


The error itself means, that a reference - in your code the reference to the component TMP_Text - has not been set.

A reference usually is either set in Start or over the editor. Since in your posted code there is no Start, it has/ should be done over the editor. Now what usually happens is that while you do set it over the editor, a second copy of the script is still somewhere attached to a different GameObject, either a child or a totally different GameObject.

The fast way to find those is to simply add a console print statement that tells you the name of the GameObject you attached the script.

Debug.Log("Attached to: " + gameObject.name);

Either putting that line to the Start or the beginning of your calling method to make sure it gets executed.

  • \$\begingroup\$ Just as a small hint: In Unity, clicking the exception does highlight the object it is attached to! It only knows this on function call level though. If the Update() method of obj A calls something in obj B and causes an error there, A will be highlighted, not B. \$\endgroup\$
    – AlexGeorg
    Commented May 2 at 13:59
  • \$\begingroup\$ Awesome little tip! Thanks so much for the assist! \$\endgroup\$
    – Skittles
    Commented May 3 at 3:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .