Caching components

I’ve been trying to optimize my iOS game as much as possible.

I’ve gotten into the habit of always caching transforms so that I’m never calling “.transform” or “transform.[whatever]” in an Update function. Instead, I assign the transform to a variable in the script’s Awake or Start functions and just call that variable in the Update functions.

Should I be doing the same for other components? Like…rigidbody, or animation? Or is transform the only one that can slow things down?

And what kind of speed increase should I even expect while doing this? I haven’t been able to really quantify how fast caching the transform component is. Does it really make a big difference?

Yes, the issue is GetComponent, not Transform.

Depends on how often you’re accessing the component. If it’s several times per frame, then it makes a difference, otherwise not so much.

–Eric

GetComponent can be cached as well so you shouldn’t have much of a problem caching what you need - anything where you’d normally use getcomponent or transform (which is a component I suppose).

transform.whatever is short for GetComponent(Transform).whatever.

–Eric