[C#] Public variable does not appear in unity if type is kind a interface create for me.

Hey guys, how are you?

I need to link class of a kind in unity, using the drag-and-drop of unity. For this I created an interface to accept more than one class. Making the code stay that way:

In the line 8, I declare the variable for accept StatesInterface.
But not appears in the Unity.

2264641--151593--Captura de Tela 2015-08-24 às 14.14.23.png

Unity doesn’t serialize variables that reference C# interfaces.

You could declare the variable as a MonoBehaviour and add a property that casts it as StatesInterface:

public class TextController : MonoBehaviour {
    public Text text;
    public MonoBehaviour states;

    public StatesInterface statesInterface { get { return states as StatesInterface; } }
    ...
}

If you want to validate that the drag-and-drop is a valid StatesInterface, you can write a custom property drawer for that variable.

1 Like

OnValidate also works for custom validation.

1 Like