I heard that the GetComponent function is heavy. Does Performance improve by setting SpriteRenderer component in advance from Inspector?
Hello there,
The GetComponent<> call is only noticeable if you call it often (like in update).
If you use it to assign your variables on Awake(), Main() or Start(), it’s totally fine.
In the end, it really depends on if you’d rather assign things in the inspector or do it through code.
Hope that helps!
Cheers,
~LegendBacon
In my experience, using a SerializedField is useful when references to other gameobjects are required and though can be set by e.g. the level designer.
The GetComponent is less error prone in cases where a script requires a conponent
If you want to avoid both, the GetComponent call, AND setting the references manually, you can use a [SerializeField] attribute and fill it with the Reset magic function.
public class MyClass : MonoBehaviour
{
[SerializeField]MyComponent component;
void Reset()
{
component = GetComponent<MyComponent>();
}
}
This way you can find the component any way you want. The search will be called whenever you add the component to the GameObject or when you call reset on it.
@Legend_Bacon
Thanks, Thanks, Thanks!
Hello LegendBacon!
You solved my anxiety and You saved me from an inefficient future.
I hope that a wonderful future will come to you as well.