Get Component every time? Everywhere?

There’s a lot of GetComponent<> in @sampattuzzi 's code… when building a project of this scale, wouldn’t it perform better if we get the component just once in the start method, then reference that object when we want?

1 Like

If the component exists on the same GameObject, and thus is guaranteed not to change, absolutely. He’ll touch on some of this later in the course. Old hacks like me are just in the habit of automatically caching our references in Awake no matter what the coursework says. Lots of the code that we’re working with here will be shifting and changing before the course is over, but I encourage you to look for these types of optimizations. It’s a good habit to be in!

3 Likes

But I would also note that GetComponent is very efficient. If there’s any chance a component might get added or removed at runtime it might be dangerous to cache it.

2 Likes

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

Privacy & Terms