A Simple GetComponent question...


We know that it is recommended to use the method below in the start() function instead of using directly the “transform” attribute in the update() function, because it reduces the time complexity:


But what about the GetComponent<…>(); method?

Assuming we have a component, called “Component One”, and assume that we have another component called “Component Two”, I write the method below in the start function of the “Component ONE”:


Will Component Two always get the updated properties of the ComponentOne? even it is called in the start() function and not in Update() function?

First of all,


doesn’t do anything since you neither store the reference to ComponentOne in a variable nor try to access a member of it. I assume you were looking for

myComponentOne = GetComponent<ComponentOne>();

And yes, as a reference type it is sufficient to store the reference once in Start() and as long as the ComponentOne component exists, access the variable myComponentOne will directly reference the actual ComponentOne on the gameObject. For further information, look up reference types vs. value types.