动态变化的文本

Dynamically changing text

我有一个名为 txtUsername 的文本组件,我这辈子都无法将其更改为显示 "Ryan."

这是我试过的:

Linked the text component to the variable user like this.

我的 JS 脚本:

var username = "Ryan";
var user : UI.Text;


// set username text
function SetUsernameText() {
user = GetComponent(UI.Text);
user.text = username;
}

您不需要使用 GetComponent(UI.Text),因为您已经在检查器中附加了该游戏对象,试试这个:

var username = "Ryan";
var user : UI.Text;


// set username text
function SetUsernameText() {
user.text = username;
}