I’ve been perusing the code of some scripts from a few assets from the Unity asset store and I’ve noticed sometimes components of the ‘gameObject’ will be cached (usually the transform). Something like:
private Transform thisTransform;
void Awake () {
thisTransform = gameObject.GetComponent<Transform>();
}
(for example)
From what I’ve read, this was common practice in Unity 3 and earlier as ‘GetComponent’ was a costly function (and gameObject.Transform is a ‘get’).
Is it still good practice to do this? Is there any noticeable performance boost or are these just code relics from the old days? I’m not one to fuss over every smitch of performance but I would like to make an informed decision on whether or not to practice this in my own code.