Question to CharacterController

Hello, I saw this:

And I wonder why GetComponent has to be in Update function and not in the (start function)?

Isnt the variable controller linked to the charactercontroller?

So why to GetComponent every frame?


Components are class instances and therefore are reference types. That means the variable is "linked to" the component. A lot of examples in the scripting reference are not "optimised".

In general it's better to use GetComponent in Awake() or Start() to get the reference once and use the variable in Update(). Anyways, GetComponent isn't that bad as FindObjectOfType (as far as i know) but it still have to iterate through all components on that GO to find the right one.

ps. The shortcut properties like transform, rigidbody, collider, renderer, ... they all use GetComponent internally. They are just there to have easy access to the most common components.