Using [SerializeField] for referencing SurfaceEffector

Hi Jasin,

The significant difference is a) performance and b) that FindObjectOfType returns a “random” object, not necessarily the one you want.

Assigning the reference to the field manually is more performant than having a method search the entire scene for an object. Assigning the reference manually also gives you full control over the value of the field. You know exactly which object your field references. With FindObjectOfType you might get a problem if you have more than one SurfaceEffector2D object in the scene but need a specific one.

FindObjectOfType is mainly used for convenience or when we cannot assign a reference manually because the object we want to reference gets generated during runtime.

Long story short: Your approach is perfectly fine. :slight_smile:


See also:

1 Like