- Home /
Basic: Text sub-component of sprite prefab not showing
Hi guys,
I'm using Unity to develop a 2d game, and have a very basic question. I have a prefab which is made of a 2d sprite, which I instantiate like this:
Instantiate (GameResources.shared.prefab_tile, new Vector2 (x, y), Quaternion.identity) as GameObject;
That works and my sprite it displayed correctly. Now I need to display some dynamic text (a number) on top of the sprite. I tried adding a Text or GUIText component to the prefab, set its text (statically from the designer, to start with), font, color etc....all to no avail, the text just does not render.
I think I'm missing something very basic, but I've already wasted a couple of hours and scoured the web, without finding a solution :( Any help is greatly appreciated!
btw, this happens even by just adding a sprite to the scene in the designer, adding the Text component and setting its properties => text does not appear
I tried adding a Text$$anonymous$$esh as well, but it doesn't work because the editor tells me that the $$anonymous$$eshRenderer component conflicts with the existing SpriteRenderer.
So basically I tried adding a Text, GUIText and Test$$anonymous$$esh component to the sprite, and none worked for me...still not sure why...
A Unity 4.6+ UI Text component requires a Canvas to render. This is probably what you should be using, as opposed to the alternatives you mentioned trying. Those alternatives are inferior to the Text component. The object must have a Text component, a Canvas Renderer component, and a RectTransform (plain Transform might work too? not sure). This object with those three components must be a child of a Canvas object.
Try creating a canvas and a text object via the editor GameObject menu and see if you can get that to show up. There are many factors in play here, and they've all got to be correct; luckily the built-in system for adding objects to the scene sets up most things automatically.
Same problem here, did you find any solution? By the way, AlwaysSunny, adding a Text component or creating a Text GameObject automatically adds Canvas Renderer component and RectTransform component as well, but it doesn't solve the problem. As marcohills said, probably just something very basic is missing. :(
This might be some kind of bug. I've since encountered an issue where duplicating objects with Text components in their hierarchy will cause the duplicate's Text component(s) to stop rendering. Nothing I can think of to try fixes them, and I've been required to duplicate such objects the "long way" by re-building them from scratch. Not sure if it's related, but seems to be. So far this issue hasn't affected any runtime-instantiated prefabs involving Texts, and is more of an annoyance than an emergency.
Answer by ZuPe2112 · May 21, 2015 at 09:40 AM
I've finally found how to fix it:
First of all, i instantiated a new empty gameobject as a child of the prefab. Then i added a textmesh component to that gameobject (so the conflict with parent spriterenderer has been avoided). It was necessary to set z coord of the text to a value near to underlying prefab z coord value (if z value is too different, text will move on the sprite as the same sprite changes its position on the screen). After that, unfortunately, i experienced another problem: text was visible only when the sprite was on a certain part of the screen (it seemed like text was visible when sprite x coord was > 0). After some research, i've found that the problem was about the sprite shader (you can find it clicking on material): i created a new material, setting shader to Unlit -> Transparent/Cutout and replaced old material with the new one.
Everything seems to work fine now :)