当我在游戏中收集电源时,我想在启动位置实例化动态文本(例如“导弹”或“盾牌”)但我的代码不起作用。
这是我的功能(我承认它有点乱):
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;
}
答案 0 :(得分:2)
看起来您没有设置正确的文字。您正在设置upgradeTxtPrefab
的文字,但您已实例化tempUpgradeTxt
应为tempUpgradeTxt.text = _upgradeTxt;