A field initializer cannot reference the non-static field, method, or property 'Foo'

Fields can only be initialised with values that are ensured to exist when the class is initialised.
This means you cannot reference non-constant, or non-static data, when initializing a field outside of a method.


Initialize your field in Awake or Start instead.

public class Example : MonoBehaviour
// 🔴 You cannot initialize a field in this manner.
private MeshRenderer _incorrectField = GetComponent<MeshRenderer>();

// 🟢 Instead initialize it in a function like Awake or Start.
private MeshRenderer _field;

public void Start()
_field = GetComponent<MeshRenderer>();