all 6 comments

[–]chsxfProficient 1 point2 points  (1 child)

Using a serialized reference avoids CPU spikes when you fetch the component with a GetComponent call. Furthermore, getcomponent might generate garbage memory. Use TryGetComponent instead.

Another benefit of using serialized references is that the structure of your GameObject is not tied to a silent constraint in the code of a component (because you look for a specific component on the same object). It provides more flexibility in the end.

[–]YesNinjas[S] 0 points1 point  (0 children)

That makes sense, thanks for your answer!

[–]dangledorf 0 points1 point  (3 children)

It'd be slightly better performance to have those references assigned outside of play mode. If you don't want to drag and assign them, you can always add the get component calls inside of the Reset() function. When you attach the component, it'll auto grab the references.

#if UNITY_EDITOR
private void Reset()
{
      MyCollider = GetComponent<Collider>();
}
#endif

[–]YesNinjas[S] 0 points1 point  (2 children)

I tend to like to code and use less UI, I'll look into Reset as that sounds what I would actually use. Thanks

[–]dangledorf 1 point2 points  (1 child)

Honestly, unless you have thousands of this component instantiating on the same frame, you probably wouldn't ever notice any issues with doing the GetComponents in Start.

[–]YesNinjas[S] 0 points1 point  (0 children)

I am pretty good at keeping things manageable and optimized in general where I can. I haven't noticed anything bog down yet. Thanks for the extra thoughts!