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 :frowning:
Any help is greatly appreciated!

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 :slight_smile: