Transform doesn't need GetComponent?

I’ve noticed in some of the videos we use a transform variable without running the below in the Startup() Method:

transform = GetComponent();

All other Components have to be stored as variables in the Startup() method before they are available for access so I just curious to know why transform is different and if there’s other components that work this way?

Thanks!

transform is pretty fundamental to GameObjects so it makes sense that Unity stores an internal variable so you don’t have to perform the GetComponent method to search for it.

Yeah, there’s a fair few of them. If you’re that interested, I suggest digging through the Unity documentation on Monobehaviour.

I’ve heard it said (I believe Rick said it in one of the lectures) that every object in Unity has a Transform property.

Could this be why (in the case of Transform)?

Yes, it would be a good reason why.

Little nit pick… Every GameObject in Unity has a transform, not every object.

The Monkees GIF, Peter_ Why can't I play smart once in a while_, from 'The Case of The Missing Monkee' episode #themonkees #gif #PeterTork #gif (1)

1 Like

In the past, there were even more “magical” variables: for example, rigidbody and audiosource, if I remember correctly. While those have been obsolete for many years, transform still exists. If you try to name your variables rigidbody or audiosource, you sometimes get a warning message because they still exist somewhere in Unity’s source code but you are not supposed to use them anymore.

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

Privacy & Terms