Putting text over a game object

I’m unsure how I could get my text to appear over an object. My project is 2.5D and when I hit a trigger point, some text comes up, which is a button prompt to interact with something. I’ve made a simple wooden sign sprite, and I would like for the text prompt to appear on the sign. This way, this slight tutorial prompt is a part of the game world. How would I go about doing this? As an aside, it’s not a typical text object, but TextMeshPro text.

Thanks

Not really a scripting question…

One way to do it would be to create a UI Canvas with its render mode set to “World Space”. This creates a UI that’s located at a specific place in your world (rather than a specific place on the screen), and you can put text, images, or any other UI elements you want on it.

I believe TextMeshPro also has a concept of text elements that exist directly in worldspace without using a canvas, but I haven’t used them before.

Yeah, it’s not a scripting issue, though I figured something could be done via a script. Plus, I find I get responses in the Scripting section compared to the other sub forums.

I’ll try that though. Thanks.

Isn’t everything here(in the software world) a scripting question though? even if you’re just interacting with it’s UI, it’s still a script. :wink:

anywho, hehe.

here’s what you do, see this code right here?

            Bounds bounds = rend.bounds; //Get a vector3 array ("corner") that encapsulates the renderer, "rend" is the renderer we are using
            corner [0] = cam.WorldToScreenPoint (new Vector3 (bounds.center.x + bounds.extents.x, bounds.center.y + bounds.extents.y, bounds.center.z + bounds.extents.z));
            corner [1] = cam.WorldToScreenPoint (new Vector3 (bounds.center.x + bounds.extents.x, bounds.center.y + bounds.extents.y, bounds.center.z - bounds.extents.z));
            corner [2] = cam.WorldToScreenPoint (new Vector3 (bounds.center.x + bounds.extents.x, bounds.center.y - bounds.extents.y, bounds.center.z + bounds.extents.z));
            corner [3] = cam.WorldToScreenPoint (new Vector3 (bounds.center.x + bounds.extents.x, bounds.center.y - bounds.extents.y, bounds.center.z - bounds.extents.z));
            corner [4] = cam.WorldToScreenPoint (new Vector3 (bounds.center.x - bounds.extents.x, bounds.center.y + bounds.extents.y, bounds.center.z + bounds.extents.z));
            corner [5] = cam.WorldToScreenPoint (new Vector3 (bounds.center.x - bounds.extents.x, bounds.center.y + bounds.extents.y, bounds.center.z - bounds.extents.z));
            corner [6] = cam.WorldToScreenPoint (new Vector3 (bounds.center.x - bounds.extents.x, bounds.center.y - bounds.extents.y, bounds.center.z + bounds.extents.z));
            corner [7] = cam.WorldToScreenPoint (new Vector3 (bounds.center.x - bounds.extents.x, bounds.center.y - bounds.extents.y, bounds.center.z - bounds.extents.z));

//find the actual square that surrounds the renderer from the 8 corners we have
            float minX = corner[0].x;
            float maxX = corner[0].x;
            float minY = corner[0].y;
            float maxY = corner[0].y;
            for (int i = 1; i < 8; i++) {
                if (corner [i].x < minX)
                    minX = corner [i].x;
                if (corner [i].y < minY)
                    minY = corner [i].y;
                if (corner [i].x > maxX)
                    maxX = corner [i].x;
                if (corner [i].y > maxY)
                    maxY = corner [i].y;
            }

            ((RectTransform)transform).position = new Vector2 (minX, minY); //set our (the UI element) position to be on the bottom left corner (our pivot must be the same as this, if you don't want to use the bottom left corner on your UI than change this line).
            ((RectTransform)transform).sizeDelta = new Vector2 (maxX - minX, maxY - minY); //set our size to fill the area.

you have some more info here, along with some screen shots and even a unity package (do note, if you download it, you need to change the canvas scaling to constant, which was the problem the thread started around).

Your canvas must be on constant size rather then scale with screen size

forgot to mention, lol.
to get your text ontop the object attach a text element(be it the legacy UI text or TMP) as a child to the UI element the script lives on and set it’s size to stretch with width and position him ontop the element.

Setting the canvas to World Space has done the trick for me, so thanks for that Antistone. :slight_smile:

@SparrowsNest I’ll look at trying that script out too, so thanks for that. What would I have to attach that script to, the canvas or the camera…?

The ui element itself, the problem with the world space cancas is that you need one canvas per thing your displaying, if you just have a couple at a time its fine