团结/ C#查找对象并获取组件 [英] Unity/C# Find object and get component

查看:348
本文介绍了团结/ C#查找对象并获取组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该是轻松的一年:

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 Components attached to it.

您可以带走 GetComponent 呼叫,只需用查找(Cubey) <结果/ p>

You can take away the GetComponent call and just use the result of your Find("Cubey")

GameObject myCube = GameObject.Find("Cubey");

这篇关于团结/ C#查找对象并获取组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆