Unity2D: Add text to rigidbody2D element

465 views Asked by At

I'm new to Unity and I started with the Catch Game Tutorial to learn to create a 2D game. Everything is working now but for my needs, I would like to add different textboxes to each of the fallen elements (in the tutorial the bowling balls) Those texts should move with the objects. I know I can change text dynamically in the code, but I couldn't figure out how to correctly add the element.

I tried to add a text object as a child of the gameobject and also tried to create a new gameobject which contains a text, but I can't position the element in front of the background and above my wished element (I can't choose a sorting layer for this)

Imagine this is the object which has to be collected and I would like to show a text like this:

enter image description here

My Questions are:

1. How can I add a text to be displayed in the correct position (GUIText, text in a gameobject, only text or something else?

2. How can I make this text dynamically move with the fallen object?

3. Can I set a background to my text as displayed above?

Thank you in advance!

1

There are 1 answers

0
user2550641 On BEST ANSWER

I found a solution for my problem and did the following thing:

  1. I created a Sprite with my wished background (black rectangle) and added it to the scene

  2. I created a 3D Text and added it as a child to the created Sprite (and scaled it and positioned it)

  3. I added a Script to the 3d Text with the following content:

    void Start () {
        this.gameObject.GetComponent<Renderer>().sortingLayerName = "[MyLayerName]";
        this.gameObject.GetComponent<Renderer>().sortingOrder = 3;
    }
    
  4. I added the Sprite (with the text as child) to my gameObject (ananas)

  5. A renewed the object in the Prefabs folder

Maybe my solution helps other people facing a similar problem.