- Home /
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.
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.
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;
}
}
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.
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.
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.
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.
Your answer
Follow this Question
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