Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Why isn't gameObeject.GetComponent<Text>(); working?

Discussion in 'UGUI & TextMesh Pro' started by MJG1123, May 6, 2015.

  1. MJG1123

    MJG1123

    Joined:
    Apr 12, 2015
    Posts:
    24
    This is what I was suggested to use to display text from code (so that it could change according to a variable) onto a per-existing GUI text Object, it just doesn't seem to be working?

    Screenshot (13).png Screenshot (12).png
     
  2. merlock18

    merlock18

    Joined:
    Dec 30, 2013
    Posts:
    28
    I just added the Text object as a data member to my code and dropped it in.
     
  3. merlock18

    merlock18

    Joined:
    Dec 30, 2013
    Posts:
    28
    Also:
    gameObject.GetComponentInChildren<Text>();
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you're getting a null reference exception at the bottom... what is that referring to?
     
  5. MJG1123

    MJG1123

    Joined:
    Apr 12, 2015
    Posts:
    24
    Solved Thank you
     
  6. Cromfeli

    Cromfeli

    Joined:
    Oct 30, 2014
    Posts:
    202
    Do you mind sharing what was the problem and how you solved it?
     
  7. MJG1123

    MJG1123

    Joined:
    Apr 12, 2015
    Posts:
    24
    I used this instead
    Screenshot (15).png
     
  8. Djfeeler

    Djfeeler

    Joined:
    Oct 11, 2014
    Posts:
    17
    You must access to the GameObject first, have a reference to an GameObject.
     
  9. CST4027

    CST4027

    Joined:
    Sep 27, 2023
    Posts:
    1
    i have the same issue and need help to fix it this is my script:


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

    public class Souls : MonoBehaviour

    public static int SoulCount;
    public GameObject SoulDisplay;
    public int InternalSoul;

    void Update ()
    InternalSoul = SoulCount;
    SoulDisplay.GetComponent<Text>().text = "Souls: " + InternalSoul;

    }
    }

    if anyone could help it would be great