Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity/C# Find object and get component

This should be an easy one:

GameObject myCube = GameObject.Find("Cubey").GetComponent<GameObject>();

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()

Normally the errors Unity displays are useful, but this is just confusing. Are cubes not GameObjects? Any pointers would be appreciated (no pun intended).

like image 778
Ghoul Fool Avatar asked Dec 16 '22 01:12

Ghoul Fool


1 Answers

GameObject is not a component. A GameObject has a bunch of Components attached to it.

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

GameObject myCube = GameObject.Find("Cubey");
like image 121
Zach Thacker Avatar answered Jan 01 '23 20:01

Zach Thacker