• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by dundee · Nov 17, 2013 at 03:31 PM · textmeshmeshfilter

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Xelareip · Nov 18, 2013 at 05:38 PM

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 : http://answers.unity3d.com/questions/526232/rotate-bullet.html)

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dundee · Nov 20, 2013 at 01:54 AM 0
Share

Thanks for the directions, will try this out. But if you don't $$anonymous$$d 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"?

avatar image Xelareip · Nov 20, 2013 at 03:52 PM 0
Share

Well, _childTextRenderer is a Renderer so it doesn't have a ".text" member. You need to have the Text$$anonymous$$esh associated with this renderer to change the text value. Best way to do so is probably :

 class ParentScript : $$anonymous$$onoBehaviour
 {
     public Renderer _childTextRenderer;
     public Text$$anonymous$$esh _childText$$anonymous$$esh;
  
     private void ChangeTextVisibility(bool state)
     {
         _childTextRenderer.enabled = state;
     }
 
     private void ChangeText(string newText)
     {
         _childText$$anonymous$$esh.text = newText;
     }
 }

And you drag&drop the same child object on the _childText$$anonymous$$esh field in the inspector.

Don't forget that when you do this drag & drop you don't necessarily reference a whole GameObject but probably one of his components. So, when you drag&drop your child object onto the _childTextRenderer field, you actually reference the Renderer component of your GameObject, not the whole GameObject.

About the prefab/component/instantiation question, I'll try to explain from experience : when you create a prefab, it's made of the root GameObject AND all of his children (and components). In the inspector fields of the GameObjects of this prefab you can reference any other object or component of the prefab since they can't exist without each other. When you instantiate the prefab, you instantiate the root GameObject, all the children, all the components AND all the links between GameObjects/Components of the prefab. What you can't do is put an instance of a prefab in your scene, reference an other object by drag&drop and try to apply it to the prefab : the other object only exists in the context of this specific scene and thus can't be used for an other instance of the prefab.

Not sure that I'm clear here ^^

avatar image dundee · Nov 23, 2013 at 12:46 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there a way to get access to the mesh geometry in a TextMesh? 1 Answer

How do I change a TextMesh's Offset Z value in JavaScript? 1 Answer

How can I achieve advanced text effects? 1 Answer

Using Text Mesh to display instructions, story, etc. 0 Answers

Unity Meshes for C# generated objects 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges