【发布时间】:2020-07-29 23:11:23
【问题描述】:
我尝试从一个游戏对象脚本、另一个游戏对象脚本调用一个函数,但我需要它使用字符串,但是当我尝试这样做时
GameObject.Find("Player").GetComponent("myscript").myfunction= false;
unity 在我可以运行之前给了我以下错误
Assets\GenericActor.cs(94,40): error CS1061: 'Component' does not contain a definition for 'myfunction' and no accessible extension method 'myfunction' accepting a first argument of type 'Component' could be found (are you missing a using directive or an assembly reference?)
【问题讨论】:
-
您需要将
GetComponent的结果转换为脚本的类型。 -
对不起,你能详细解释一下吗?
-
GetComponent返回一个GameObject类型的对象。GameObject没有名为myfunction的方法。您需要将GetComponent的结果转换为myscript类型。 -
如果不清楚,请查看 typecasting。
-
@3Dave 如果我错了,请原谅我,我有点生疏了,难道没有通用形式的 GetComponent 吗?像 .GetComponent
("my script")?