Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Prabhu E · Oct 31, 2013 at 07:40 PM · gameobject3dtext

How to add 3D text to the moving GameObject ?

I have a GameObject that is Instantiated at Runtime. Here I want to attach 3D Text to that Instantiated GameObject and that Text move along with the moving GameObject.

Here is my Code:

 void Update()
     {
        GameObject gameObj = (GameObject)GameObject.Instantiate(myPrefab);
        GameObject _3d = (GameObject)GameObject.Find("txt");
        gameObj.transform.position = new Vector3(Random.Range(this.transform.position.x - 1, this.transform.position.x + 1), Random.Range(this.transform.position.y - 0.1f, this.transform.position.y + 0.1f), 0);
     
        // Here I tried to attach 3D Text
 
        // My Code is
        /*
            TextMesh t;
            t.transform.position = gameObj.transform.position;
            t = _3d.GetComponent<TextMesh>();
            //t = (TextMesh)_3d.GetComponent(typeof(TextMesh)); 
            t.text = "My New Text";
        */
 
     }

But this doesn't add any Text to the gameObj. Help is needed. Thanks in Advance.

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 robertbu · Nov 01, 2013 at 12:24 AM

The following code will add text to an game object. Before integrating it into your code, verify it works for you in a new scene:

  • Drag a font into Unity

  • Create an empty game object at the origin

  • Add the script below to the game object

  • Select the empty game object in the hierarchy

  • Drag the font you created in the first step onto the 'font' variable in the Inspector

  • Open up the font you added (click on the triangle). Reselect the empty game object and drag the material inside the font onto the 'material' variable in the Inspector.

  • Run the app to test


    pragma strict

    var font : Font; var material : Material;

    function Update() { if (Input.GetKeyDown(KeyCode.A)) { gameObject.AddComponent(MeshRenderer); renderer.material = material;

           var tm = gameObject.AddComponent(TextMesh);
             
             tm.text = "Some text to display";
             tm.font = font;
             tm.characterSize = 0.25;
         }
     }
    
    

It might be easier to create a TextMesh prefab and then Instantiate() prefab and make it a child of the moving game object.

Comment
Add comment · Show 7 · 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 Prabhu E · Nov 01, 2013 at 06:01 AM 0
Share

One doubt. I Integrate this code to the script which is attached to the GameObject. $$anonymous$$aterial and Font is None in that. (Not working if I leave it in the Inspector). And it says

Can't add component '$$anonymous$$eshRenderer' to Test because such a component is already added to the game object! Can't add component 'Text$$anonymous$$esh' to Test because such a component is already added to the game object!

"Test" is Empty GameObject created as per your procedure.

$$anonymous$$y script would be

 public class InstantiateObject: $$anonymous$$onoBehaviour
 {
     public $$anonymous$$aterial material; // None in Inspector
     public Font font; // None in Inspector
     void Update()
     {
          GameObject g = (GameObject)GameObject.Instantiate(myPrefab);
          GameObject text = (GameObject)GameObject.Find("Test");
          g.transform.position = new Vector3(Random.Range(this.transform.position.x - 1, this.transform.position.x + 1), Random.Range(this.transform.position.y - 0.1f, this.transform.position.y + 0.1f), 0);
          text.transform.position = g.transform.position;
                 
          text.AddComponent<$$anonymous$$eshRenderer>(); // Throws Error
          renderer.material = material;
 
          Text$$anonymous$$esh tm = text.AddComponent<Text$$anonymous$$esh>(); // This too throws Error
 
          tm.text = "Some text to display";
          tm.font = font;
          tm.characterSize = 0.25F;
       }
 }
avatar image robertbu · Nov 01, 2013 at 06:23 AM 1
Share

You have some problems here. First, you are executing this code in Update(). This means that you will get a new game object each frame. Second, '`GameObject.Find("Test")`' will return the first one found...the same one each frame. This means that after the second time through you will be adding the renderer twice. The '`renderer.material = material;`' is adding the material to the current game object, not to the text game object. So here is a quick rewrite to something that works. This is not what you are doing above, but it gives you a working start point. Attach it to an empty game object, fixup the 'font' and 'material' and hit play.

 using UnityEngine;
 using System.Collections;
 
 public class InstantiateObject: $$anonymous$$onoBehaviour
 {
     public $$anonymous$$aterial material; // None in Inspector
     public Font font; // None in Inspector
     void Start()
     {
          GameObject g = new GameObject();
          g.transform.position = new Vector3(Random.Range(this.transform.position.x - 1, this.transform.position.x + 1), Random.Range(this.transform.position.y - 0.1f, this.transform.position.y + 0.1f), 0);
          g.transform.position = g.transform.position;
  
          g.AddComponent<$$anonymous$$eshRenderer>(); // Throws Error
          g.renderer.material = material;
  
          Text$$anonymous$$esh tm = g.AddComponent<Text$$anonymous$$esh>(); // This too throws Error
  
          tm.text = "Some text to display";
          tm.font = font;
          tm.characterSize = 0.25F;
       }
 }

I'm not sure of your organization here. You seem to be adding a Text$$anonymous$$esh to a game object you find in the scene, not to the prefab you created. If this only happens once, you are fine, but the second time this code is executed, you will be adding second copies of Text$$anonymous$$eshes and $$anonymous$$eshRendeer to this 'Test' game object.

avatar image Prabhu E · Nov 01, 2013 at 05:39 PM 0
Share

Thanks a lot for your effort. I attach the text to the GameObject by your hint

It might be easier to create a Text$$anonymous$$esh prefab and then Instantiate() prefab and make it a child of the moving game object

Create the 3D Text Game object as the Child of Original GameObject and drag it into the Prefab, solves the problem.

And I have a quick question, (Assume Cube is a main GameObject and 3d text is attached to it by 'Fixed Joint'). Is it possible to remove the joint between them (at the time of Collision with wall colliders) in the script.

avatar image robertbu · Nov 01, 2013 at 05:55 PM 0
Share

You should be able to disable the joint or remove the joint (i.e. delete the component), or you can just set the break force to something reasonable and let the physics take over.

avatar image Prabhu E · Nov 01, 2013 at 07:04 PM 0
Share

Can't get it from you. For my case, Object with attached Text is drag into the prefab and then deleted from the scene view. I want to break the Text and Object (in Prefab) when OnCollisionEnter with Other Objects.(Basically I need the Object to be Destroy but 3d Text appears on the Screen). forgive me, because I didn't mentioned my problem in previous comment.

Show more comments

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.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

Instantiated enemy doesn't work. 1 Answer

Display Name Above Object 2 Answers

Insert 3d text to the front face of a cube GameObject 0 Answers

Text on Models 1 Answer

Gameobject: 3D Text, how to change text? 2 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