1

我对 GetComponent(GUIText) 有疑问,我得到的错误是

“#######COUNTER(Clone)”游戏对象没有附加“GUIText”,但脚本正在尝试访问它。

这是我的代码:

var UItecxt = GameObject.Find("#######COUNTER(Clone)");
var txtconvert = UItecxt.GetComponent(GUIText);
print(txtconvert);
txtconvert.text = counternumb.ToString();

我的克隆上有一个 GUIText!问题是什么?谢谢!

4

2 回答 2

0

您的问题不够详细,无法确定问题。

不过,我认为您提到的游戏对象应该是您实例化的预制件。

你把sciprt放在哪里或附加在哪里?确保它附加到您实例化的预制件上。

此外,您可以尝试使用直接引用组件而不是使用 gameobject.Find。您可以更轻松地在检查器中拖放元素。

于 2015-01-06T04:16:34.470 回答
0

您的问题是场景中没有名为 clone 的 GameObject "#######COUNTER(Clone)"。在下面运行我的代码,您会注意到。

var UItecxt = GameObject.Find("#######COUNTER(Clone)");
var txtconvert : GUIText;

if(UItecxt != null)
   txtconvert = UItecxt.GetComponent(GUIText);

else
  Debug.Log("There was no GameObject with the name '#######COUNTER(Clone)' in the scene");

To fix it just make sure you do have a GameObject with that name.

于 2015-01-06T09:31:10.623 回答