语法:“x”是一个“变量”,但用作“方法”
本文关键字:变量 一个 方法 语法 | 更新日期: 2023-09-27 17:56:10
public GameObject explosionPrefab;
void OnCollisionEnter(Collision otherObj) {
GameObject explosionObject = Instantiate(explosionPrefab, otherObj.transform.position, Quaternion.identity) as GameObject;
explosionObject(explosionObject, 5f); <-- THE LINE GIVING ERROR
Destroy(otherObj.gameObject);
}
}
我很困惑,因为我做脚本的时间不长,想知道是否有人可以向我展示正确的方法,这是针对 Unity 的小型测试游戏。只是想知道这是如何解决的,这样我将来就不会犯这个错误。
一般
方法声明:
void x() {
// do something
}
对该方法的调用:
x();
变量的声明:
int x = 0;
变量的使用:
x = 7; //write
y = x; //read and assing to another variable
您的代码
您声明了一个变量:
GameObject explosionObject = Instantiate(explosionPrefab, otherObj.transform.position, Quaternion.identity) as GameObject;
但是你把它当作一种方法来使用:
explosionObject(explosionObject, 5f); <-- THE LINE GIVING ERROR
如果要调用游戏对象实例的方法,您可能会错过方法名称?例如:
explosionObject.MyMethod(explosionObject, 5f);
或者,您可以对实例使用相同的名称并使用其他方法,然后尝试更改游戏对象实例的名称:
GameObject game = Instantiate(explosionPrefab, otherObj.transform.position, Quaternion.identity) as GameObject;
explosionObject(game, 5f);
问题非常清楚,让我们看看下面的代码:
explosionObject(explosionObject, 5f);
在这里,您已将explosionObject
声明为 GameObject
变量,并且您对方法使用相同的名称,这就是发生此问题的原因。更改变量名称或函数名称,您的问题将得到解决。