c# - Unity3d,无法通过字符串使用GetComponent从另一个脚本调用脚本
问题描述
我尝试从一个游戏对象脚本,另一个游戏对象脚本调用一个函数,但我需要它使用字符串但是当我尝试这样做时
GameObject.Find("Player").GetComponent("myscript").myfunction= false;
在我可以运行之前,统一给了我以下错误
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?)
解决方案
尝试:
MyScript playerScript = GameObject.Find("Player").GetComponent<MyScript>().myfunction = false;
也可以使用标签来完成:
MyScript playerScript = GameObject.FindGameObjectWithTag("Player").GetComponent<MyScript>().myfunction = false;
另外,如果你调用像 void MyFunction(bool myBool) 这样的函数,你会使用
MyScript playerScript = GameObject.FindGameObjectWithTag("Player").GetComponent<MyScript>().MyFunction(false);
推荐阅读
- r - 我们如何配置shinyserver开源来支持并发用户
- c - 如何使用 rand() 在 c 中生成具有指定长度的随机数?
- ruby - 为 Gtk2/Ruby 中的按钮添加自定义背景颜色使其具有橙色边框
- c - openssl aes evp解密多次,无法得到预期结果,为什么?
- python - 在 asyncio 模块中,如何使用我们想要继续的变量来打破事件循环
- github - 如何部署大小为 876 MB 的 github 项目?
- reactjs - 反应:父母收到的回调值没有将另一个孩子的最新值作为道具传递| 功能组件
- xpath - XPath for xquery 在每个元素上应用 where 子句
- facebook-messenger - Facebook Messenger 聊天机器人身份验证
- java - 如何在 http 标头中发送西里尔文/完成字符