在Unity中实例化动态文本

时间:2018-05-14 15:26:09

标签: c# unity3d text

当我在游戏中收集电源时,我想在启动位置实例化动态文本(例如“导弹”或“盾牌”)但我的代码不起作用。

这是我的功能(我承认它有点乱):

public Text upgradeTxtPrefab;

void ShowUpgradeTxt (string _upgradeTxt) {

        // find canvas
        GameObject canvas = GameObject.Find("Canvas");

        Text tempUpgradeTxt = (Text)Instantiate (upgradeTxtPrefab);

        tempUpgradeTxt.fontSize = 24;

        tempUpgradeTxt.transform.position = this.transform.position;

        tempUpgradeTxt.transform.SetParent (canvas.transform, false);

        upgradeTxtPrefab.text = _upgradeTxt;

}

1 个答案:

答案 0 :(得分:2)

看起来您没有设置正确的文字。您正在设置upgradeTxtPrefab的文字,但您已实例化tempUpgradeTxt

应为tempUpgradeTxt.text = _upgradeTxt;