Error with EditorGUILayout.PropertyField

I have this code

public enum TypeOfMaterial {SingleMaterial,RandomFromCollection}
  public static TypeOfMaterial MaterialType;
  public static Material[] Materials;

if(MaterialType == TypeOfMaterial.RandomFromCollection)
     {
      ScriptableObject target = this;
      SerializedObject so = new SerializedObject(target);
      SerializedProperty stringsProperty = so.FindProperty("Materials");
      EditorGUILayout.PropertyField(stringsProperty, true);
      so.ApplyModifiedProperties();
     }

But it gives me the following error when I select ‘RandomFromCollection’ on the enum, and I don’t know why…


with line 808 being: EditorGUILayout.PropertyField(stringsProperty, true);

You can’t serialize static properties… well because… they don’t belong to the instance, they belong to the class. imagine if you did serialize static properties and you had two instances with different seralized static values. that would mean deserializing one instance (simply reading the instance) would adversely affect the other.

because of this FindProperty returns null because it doesn’t look at static fields or properties

Alright so how do I approach this problem then? Because other scripts depend on that variable being static, because if I remove the static, I get ‘An object reference is required to access non-static member…’ What is the correct approach for this?

Thanks!

you can manually draw out a field to set it, but you won’t save the changes to file. as soon as you close Unity and reopen, or build your game that field is going to get cleared.

One way around this is to make a Project Asset hold the list of materials and then all your scripts reference the asset. exactly how you access the asset is up to you. you can use a singleton, find it through Resources.Load, or have a field reference to it.