Using [SerializeField] for referencing SurfaceEffector

Hi, so my first thought about reaching the SurfaceEffector was to do it by SeralizeField. Then I could simply click the dot and select the effector I needed.

image

I was wondering if there is any significant difference between doing it like that and usind FindObjectOfType. Is any of those better approach, and if so, why? I felt like “finding” the object would be more complex task than just taking the reference put in the editor, but maybe I’m wrong?

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

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms