- Home /
Is GetComponent<>() slow?
I have an array of GameObjects with a MyScript component attached to each. Every frame I need to update a value in MyScript in each of the GameObjects. Is calling GetComponent<>() each frame for each object going to be slow? Should I create a separate array of MyScript components of the corresponding GameObjects instead?
Answer by Linus · Feb 10, 2014 at 12:37 PM
See http://answers.unity3d.com/questions/185164/what-more-should-i-be-caching-within-my-scripts.html
and this one http://answers.unity3d.com/questions/458762/are-gameobject-and-transform-both-using-getcompone.html which includes examples of the performance i tried a while ago.
Ok, so I'm not sure. In my case the answer would be - no, it's not worth caching?
Yeah I'd cache it in Start or Awake as a matter of course (but I'm not sure that this is easy in your case), just for the hell of it. If you've only got a few you'll not notice.
Your answer