2013-10-22 261 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";