how to put a text above a game object?

Hi!

I’m new to developing a games and i choose to use unity game engine. I have experience in developing windows application using C#.

I’m currently doing some modification in the survival shooter game tutorial. I would like to add a cube with a label above the object.

1 Like

Add a canvas to the scene, add a Text element to the canvas, and set the position using Camera.WorldToScreenPoint with a vertical offset.

You may want a world space canvas. Which allows you to place text into the world vs as an overlay type effect. But you’ll have to see if that is what you want to consider if it works for you.

3 Likes

@ezgolucky

Hi - I think this might help you:

You can use this:

   public GameObject target;
   Rect rect = new Rect(0, 0, 300, 100);
   Vector3 offset = new Vector3(0f, 0f, 0.5f); // height above the target position

    void OnGUI()
    {
        Vector3 point = Camera.main.WorldToScreenPoint(target.position + offset);
        rect.x = point.x;
        rect.y = Screen.height - point.y - rect.height; // bottom left corner set to the 3D point
        GUI.Label(rect, target.name); // display its name, or other string
    }

One, this is a 4 year old post.
Two. OnGui is an old way of doing gui and is not the suggested way anymore.

I found this post because I had the same issue, and I solve using OnGUI.

@Brathnann can you suggest a better way to do this?

2 Likes

Just took a look at the code I use that’s working. I use screen space overlay, and call the below in LateUpdate only after the camera has moved. I actually have the camera script call a list of delegates, which then calls this code on the floating UI object.

                    if (AnchorRectTransform == null)
                    {
                        AnchorRectTransform = GetComponent<RectTransform>();
                    }

                    Vector3 screenPosition = Camera.main.WorldToScreenPoint(CameraPointAboveShip.position);
                    //Debug.Log(screenPosition);
                    if (screenPosition.z < 0f)  //Disable display when behind the camera
                    {
                        enableDisplay(false);
                    }
                    else
                    {
                        enableDisplay(true);
                    }

                    AnchorRectTransform.anchoredPosition = screenPosition;

All enableDisplay does is switch the CanvasGroup.alpha between 0f and 1f. I don’t remember if that was actually important to do or not. I use Camera.main instead of a cached reference to the camera, because in my game the player is switching between several main cameras.

1 Like

Add a Text under the target object ( target object > canvas > Text),
and then name the Text as you want if you need.

//Unity2D Label for 3D Object
GameObject my_label = GameObject.Find(“UI_Text_Name_As_you_named”);
RectTransform tr = my_label.GetComponent();
Vector2 screenPoint = Camera.main.WorldToScreenPoint(target_3d_object_pos_Vector3);
tr.transform.position = screenPoint;

Control anchors, pivots and so on as you need~

9062623--1253269--2dlabel.gif

Please don’t necro-post six-year-old threads.

Instead, start your own post. It’s FREE!

When you post, remember we cannot read your mind.

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?

Do not TALK about code without posting it. Do NOT retype code. Copy/paste and post code properly. ONLY post the relevant code, and then refer to it in your discussion.

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: Using code tags properly