Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Game Development

Ranvir Sahota
Ranvir Sahota
9,844 Points

GameObject.GetComponent vs Component.GetComponent in Unity

I was looking through the unity documentation when I noticed these two methods in separate classes though they look like they same function and description they remain to be separate methods. I couldn't find any post. So I've came here to get an answer which will explain differences between the two. Another question I have is how we call GetCompoent in the first place. We aren't referencing any class. The documentation show how its created. public Component GetComponent(Type type); but how can a method be declared with the class name in the method declaration? If this is its method deceleration.

Component: https://docs.unity3d.com/ScriptReference/Component.GetComponent.html Game Object: https://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html