TextMesh and MeshFilter in one gameObject?

To be clear, I have a (prefabbed) gameObject with capsule mesh filter in it. I was trying so that the same gameObject could display a number on top of it, related to the gameObject name. Therefore I tried to add a 3d text mesh component. But it apparently “breaks” the object, the renderer is messed up.

So is it impossible to have both component rendered in the same object?

If it is, how’s the alternate solution? Some search lead to me to this (both not tried yet, if possible please help me pick one):

  1. Make a child object with text component in it, and then at start have the parent (capsule) object tell the child it’s name so the text mesh component could be set. I actually prefer this one because my main game script doesn’t really keep a reference to all those capsules, and for some capsules, I need to hide those text (done by setting renderer.false at child). The main problem is I’m still unsure about having two object in one prefab, and how to script the telling. The text might not be seen easily if the camera rotates at some angles, too, like default for the text but 90 degree at x-axis for the camera.
  2. Assign a GUI text, which seems to eliminate the multi-object prefab and camera problem. I’m just not that good with GUIs, and how to hide the GUItext? I mean, with said 3d text, I could just set the renderer to be false at some point to hide it. But with GUI text, which is displayed with OnGUI (if I’m not mistaken), do I have to make a check at every OnGUI define whether to hide the text or not?

1 Answer

1

The best solution IMO is your first point : a child object with a TextMesh attached.
Having several object in one prefab is not a problem and can help clarifying things, just define the root object as your prefab and all children will also be part of this prefab.

As far as I know, GUITexts are heavy on draw calls and should never be used as an in game GUI tool, only for menus.

For the “telling” part (i.e. activating/deactivating the renderer) you need one script on the parent that has a public Renderer member and in the editor you drag&drop the child object on that field.

class ParentScript : MonoBehaviour
{
    public Renderer _childTextRenderer;

    private void ChangeTextVisibility(bool state)
    {
        _childTextRenderer.enabled = state;
    }
}

You can also write a script attached on the same child object to make sure it always faces the camera. (You can start with this thread for this part : Rotate bullet - Unity Answers)

Thanks for the directions, will try this out. But if you don't mind me asking again, I will be instantiating the prefab via script. Then how should I drag the child to that script? Will dragging the component in the source prefab apply to all its instantiates? I got mixed up in what will be applied to all instantiates and what won't. And actually, what I mean by telling is how the parent set the text in the child. But looking at this script, I suppose I can just use _childTextRenderer.text = "something"?

Alright, thanks. I guess I got the instantiate part now.