Cannot serialize interface with [SerializeReference]
I don't know why, but this code is just not working, the inspector won't allow me to serialize my interface.
public class OnSpawnActionsContainer : MonoBehaviour
{
public List<ISpawnedAction> Actions => _actions;
[SerializeReference]
private List<ISpawnedAction> _actions;
}
I also tried code from the API page of [Serialize Reference], just copied and pasted, but it wouldn't work either. https://docs.unity3d.com/ScriptReference/SerializeReference.html
Answer by iohouse · Nov 06, 2021 at 07:10 AM
You'll have to do just this:
[SerializeReference]
public List<ISpawnedAction> Actions;
Or something like this (following your public/private example):
public List<ISpawnedAction> Actions => _actions;
[SerializeField, SerializeReference]
private List<ISpawnedAction> _actions;
The problem here is not with the Interface... since Unity does not serialize Properties (a member with getter/setter) you'll have to compromise on how you write your code a bit (you could use some custom script to work with a serialized Property though; its just not handled by Unity by default).
Making the field public is not helping. Editor does not draw my List
I didn't mention anything about the accessor having to do with the issue - just that it's declared as a Property. Both of the examples I provided work (the one with just a public Actions declared, and the other with both a public Property and a private member variable that is serialized to be viewable in the Inspector).
Your answer

Follow this Question
Related Questions
Custom inspector for items in array. 1 Answer
Animation Ignores Script 2 Answers
How to make the same script on multiple objects work just for the object where it is called? 1 Answer
Put a GameObject in the script's inspector when the script belongs to a prefab 1 Answer
How to reference custom assets from Inspector or Script 0 Answers