2013-10-22 795 views
0

我已经阅读了很多关于如何从脚本更改3DText文本的问题。3DText - 通过脚本更改文本 - Unity

很多人提出了以下:::

GetComponent(TextMesh).text = "blah"; 

但是当我尝试使用此,我得到一个错误Expression denotes a型“其中一个variable',值”或method group' was expected

我尝试了很多的例子,并不能真正让它工作。

TextMesh textMesh; 
textMesh = (TextMesh) descriptionObject.transform.GetComponent("Text Mesh"); 
textMesh.text = "Name : ABC"; 

上面的代码虽然编译没有错误不会改变文本。有人可以帮我解决这个问题吗?如何更改3DText对象的TEXT。

谢谢...

回答

1

This Works !!!!

textMesh = (TextMesh) descriptionObject.transform.GetComponent(typeof(TextMesh)); 
     textMesh.text = "Name : ABC"; 
2

这会比一个已经给了一个漂亮的解决方案(C#脚本示例中使用):

//define a Textmesh that we want to edit 
public TextMesh tm; 

// here in start method (run at instantiating of script) i find component of type 
    // TextMesh (<TextMesh>) of object named"nameOfTheObject" and reference it 
    // via tm variable; 
void Start() { 
    tm = (TextMesh)GameObject.Find ("nameOfTheObject").GetComponent<TextMesh>(); 
      // here we change the value of displayed text 
      tm.text = "new Text u want to see"; 
} 

或者,如果ü要做到尽可能以最短(语法明智的):

//keep in mind this requires for the script to be attached to the object u 
// are editing (the 3dText); 
//same as above, the only difference is the note in the line above as this 
// method is run from gameObject.GetComponent.... 
// gameObject is a variable which would be equivalent of this.GetComp... 
// in some other programming languages 

GetComponent<TextMesh>().text ="new Text u want";