Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

.text problem I can't solve. Help!

Discussion in 'Immediate Mode GUI (IMGUI)' started by Marco_Turtle, Apr 3, 2015.

  1. Marco_Turtle

    Marco_Turtle

    Joined:
    Apr 2, 2015
    Posts:
    2
    I keep on getting this problem even though it still lets me test it. I filled out everything in the inspector and ended with this every time. I know this is probably a simple fix but i'm new to Unity. Please help! Thanks in advanced.

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

    public Text someText;
    public string gameOver = "GAME OVER";

    void Start()
    {
    someText = GetComponent<Text>();
    }

    void Update()
    {
    someText.text = gameOver
     
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    Does the game object that this script is on also have a Text component on it ?

    The component must be on the same object, and not on another object (e.g: child object)
     
  3. proandrius

    proandrius

    Unity Technologies

    Joined:
    Dec 4, 2012
    Posts:
    544
    Try
    Code (csharp):
    1. someText = GameObject.Find("ObjectThatHasTextComponent").GetComponent<Text>();
     
  4. Marco_Turtle

    Marco_Turtle

    Joined:
    Apr 2, 2015
    Posts:
    2
    Thanks for replying
    For some reason the message just went away when I deleted and rewrote
     
    Last edited: Apr 3, 2015