Caching components?

In the course, I’m noticing that most of the time a method needs to be called on a specific component, they’re just using GetComponent().Method();

Isn’t it more efficient to just cache them in the start event so that we’re not constantly grabbing the component? Especially if you’re calling the methods quite frequently? Doesn’t it add up to performance improvement as well?

Hi @CursedToast,

Good question and the answer is yes and no. It really depends on your use case.

If you’re always accessing the same object each and every time then yup it can be a good idea to cache it in the start method.

If however you need to access a different object that has the same class each time then you would want to get the reference on the fly.

Cheers,
Todd

I would tend to agree with SelfAvenger, with the notable exception that if you are caching references, if any public method relies on a cached reference, then that reference should be in Awake()

1 Like

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

Privacy & Terms