I know that there is a benefit to caching an object’s transform, for instance, as simply using “transform” uses a GetComponent call.
But what about something like Time.deltaTime, CharacterController.isGrounded, or iPhoneInput.acceleration? Is there a benefit to storing these values in local variables?
For the iPhone (probably applies to all but more important on the iPhone)?
As I understand it and while you could cache properties, the benefit comes from caching an instance of a class which prevents redundant allocations to the heap. So instead of caching a property like isGrounded, cache an instance of the class CharacterController and check the isGrounded property on the instance. In other words, no I don’t think there’s much/any benefit to caching properties like that but I could be wrong…
Is it true that calling “transform” uses GetComponent? Based on the docs it’s a property of GameObject, etc. so when you use it you’re simply using a member variable.
Caching the transform component is definately faster than calling .transform.
Calling .transform is slightly faster than GetComponent(typeof(Transform)); but caching is definately a larger improvement on top of that.
That doesn’t mean you should do it in all your game code, in most cases caching those properties doesn’t make that much sense, but if you have some game code that is really slowing things down or when you access the properties in inner loop it can have a big impact.