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. Dismiss Notice

Attach text to an unit

Discussion in 'Scripting' started by Fuzzypup, May 12, 2014.

  1. Fuzzypup

    Fuzzypup

    Joined:
    Aug 13, 2013
    Posts:
    190
    I am having a problem finding a way to attach a text to a unit on the map in a 2d game.

    To keep it simple lets say it's tank combat 2d game and a hero has many tanks on the map. Each tank has the same sprite map that I change the UV to display which ever model I want. I want to also display the tank's armor percentage as text.

    GUItext attaches to the camera from what I read so that's out.

    If I instantiate the tank there is no text.

    If I pause the game to change the text to see what's wrong the graphic UV gets all messed up for the tank.

    I did 3 hours of research to find an answer so now I am down to asking the better programmers.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    You can use a TextMesh ("3D Text") for this, or you can use GUI.Label (though you'll have to do some annoying math to get it to the right place).

    A GUIText would work, if you use Camera.WorldToViewportPoint to set its position.
     
  3. rutter

    rutter

    Joined:
    Mar 21, 2012
    Posts:
    84
    Certainly possible, but not as trivial as you might like.

    Two solutions, either of which involves some scripting:

    First option, associate each tank with a GUIText. Use a script to make sure that the text's screen position matches the tank's. Generally, you'll make use of Camera.WorldToViewportPoint.

    Second option, associate each tank with a TextMesh, and create a second UI camera that captures ONLY those text meshes after the world is rendered. Use a script to make sure that the text's screen position matches the tank's. This gives you a bit more control, but is also harder to manage.
     
  4. Fuzzypup

    Fuzzypup

    Joined:
    Aug 13, 2013
    Posts:
    190
    Thanks.
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
  6. Fuzzypup

    Fuzzypup

    Joined:
    Aug 13, 2013
    Posts:
    190
    I'll take a look at that.
     
  7. Fuzzypup

    Fuzzypup

    Joined:
    Aug 13, 2013
    Posts:
    190
    Seems like I can make an object. Move my prefab under it. Make the Text3d prefab, move it under the object. Then move the object to become a prefab. I then have a prefab with two child prefabs under it?

    If I could do that I think I can solve quite easily the text problem,
     
  8. Fuzzypup

    Fuzzypup

    Joined:
    Aug 13, 2013
    Posts:
    190
    Hmm it worked.

    #1 move Army prefab to Hierarchy
    #2 move Text prefab to Army prefab in the hierarchy
    #3 move it back to the projust

    Now the text follows it around and all I have to do is modify it?!?!

    I thought you couldn't create a child in a prefab?
     
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    There's never been a problem having children in prefabs. You may be thinking of nested prefabs, which isn't the same thing.

    --Eric