0

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

他们中的很多人提出了以下建议:::

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。

谢谢...

4

2 回答 2

3

这将是一个比已经给出的更漂亮的解决方案(示例中使用的 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";
于 2014-05-12T17:14:41.717 回答
1

这有效!!!!

textMesh = (TextMesh) descriptionObject.transform.GetComponent(typeof(TextMesh));
        textMesh.text = "Name : ABC";
于 2013-10-22T09:48:39.743 回答