问题描述
这应该是轻松的一年:
GameObject myCube = GameObject.Find("Cubey").GetComponent<GameObject>();
刚刚踢了错误CS0309:类型UnityEngine.GameObject必须转换为UnityEngine.Component以在通用类型或方法UnityEngine.GameObject.GetComponent使用它作为参数T()
just kicks up error CS0309: The type UnityEngine.GameObject must be convertible to UnityEngine.Component in order to use it as parameter T in the generic type or method UnityEngine.GameObject.GetComponent()
一般情况下,误差统一显示器是有用的,但是这仅仅是混乱。是不是立方体GameObjects?任何指针将不胜感激(没有双关语意)。
Normally the errors Unity displays are useful, but this is just confusing. Are cubes not GameObjects? Any pointers would be appreciated (no pun intended).
推荐答案
游戏对象
不是一个组件。 A 游戏对象
有一堆组件
取值连接到它的。
GameObject
is not a component. A GameObject
has a bunch of Component
s attached to it.
您可以带走 GetComponent
呼叫,只需用查找(Cubey)
You can take away the GetComponent
call and just use the result of your Find("Cubey")
GameObject myCube = GameObject.Find("Cubey");
这篇关于团结/ C#查找对象并获取组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!