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

Attaching text to a GameObject?

Discussion in '2D' started by wcburg1, Mar 23, 2018.

  1. wcburg1

    wcburg1

    Joined:
    Mar 23, 2018
    Posts:
    19
    Hi all, I am trying to figure out how text works in Unity 2D and am currently stumped. What I'm trying to do is attach a Text component to a game object (specifically a sprite) and have that text move with the game object. The text will also be manipulated by the script of the game object. So far I can't even get the text to display. I have a text component hooked up to my game object but no matter what I adjust it doesn't come up on the scene, even if I change the font color and everything. How do I even get it to display? Thanks for the help. Also this is all in 2D.
     
    stiteler and damobe like this.
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    You need to attach a Canvas to your GameObject. This Canvas should be in World Space. Then you can add the Text to this Canvas.
     
    ant-papa, stiteler, Samura and 4 others like this.
  3. wcburg1

    wcburg1

    Joined:
    Mar 23, 2018
    Posts:
    19
    Thanks for the reply... I attached a canvas component to the gameobject and a text object also but the text still isn't showing up on screen. I have world space checked and everything. Here is a screenshot of the inspector window for this gameobject.
     

    Attached Files:

  4. EdKirby

    EdKirby

    Joined:
    Nov 15, 2013
    Posts:
    43
    Do you have another canvas in the scene? If you have another canvas but it's render mode is Screenspace Overlay, it could be blocking the text. If you have another canvas and it's render mode is Worldspace, then it could be a sorting layer/order in layer issue. Without seeing how your scene is made up it's hard to say.
     
  5. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    You want to place the Text as a child object to the Canvas. It needs a Rect Transform to place it in the correct position.
     
  6. XDE4NX

    XDE4NX

    Joined:
    Jul 28, 2021
    Posts:
    1
    hey how can i extract that text and use it to display on another TMP??
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,621
    Please create your own threads for questions rather than hijacking existing ones. Also, this is the 2D sub-forum, UI related questions should go here.

    Thanks.
     
  8. miharbico

    miharbico

    Joined:
    Apr 30, 2022
    Posts:
    2
    make the order in layer higher number try 100