Knowing which components will allow us to use GetComponent

I was a bit surprised that, when using the RayCastHit structure, we had to go via .transform or .collider in order to get access to the method GetComponent(). It’s in the documentation so I was able to find it, as part of the challenge. Documentation is great.
I was just surprised to see that it was the transform component that enabled using this method. Rather than, I dunno, .gameobject or something more general. transform is just about position and shape and whatnot - I didn’t quite understand how or why the transform would ‘know how’ to use GetComponent.

Is the transform component of game objects something that ‘stores’ an awful lot of functionality? Is this particular to RayCasHit structures, or is .transform generally quite handy for enabling calling methods?

Thanks. I hope this question makes sense!! X-D

AFAIK it’s typically anything that’s gameObject, but to be a gameObject, you must also have a transform, so it makes sense that both have access.

Any component that is, itself, attached to a GameObject can call GetComponent<T>() to get another component on the same GameObject. This includes transforms and even other things you wouldn’t think of like Animators, Mesh Renderers, and Rigidbodies…

What this also means is that once you have a reference to one component on another GameObject, you can use that component’s GetComponent to get other components on the GameObject.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms