The Inspector shows and allows editing of two types of properties: Values and References. Values include all primitive types like floats, ints, strings. References include things like GameObjects, Components, etc. At a glance, it looks like the only reference types that the Inspector can show are those from the UnityEngine package.
Except a struct is a reference type. And Unity can “inspect” value types just fine. Transform is a value type.
[System.Serializable]
class Test
{
public int p = 5;
public Color c = Color.white;
}
Any instance of Test will now be accessible or “inspectable” in the unity Editor (public fields only). Fairly sure System.Serializable works for structs too.
actuallly not a big deal but any idea why the default values are not working ?
basically with for example :
[System.Serializable]
public class GameEntity
{
public float posX = 4.0f, PosY = -8.0f;
}
I was expecting to have my array intiliazed correctly with these values but it’s actually not working as PosX and PosY have a value of zero in the inspector
As far as I know, it’s just an idiosyncrasy of Unity; for some reason or other, when an array of objects of a custom class type is created, field initializers are ignored and the fields are all initialized to their default values.
I’m not sure if that’s it. When you resize an array in the inspector, I think Unity creates objects as necessary for you. It’s just that for some reason, the field initializers for said objects (assuming they’re of a class type) are either overwritten or ignored. (Maybe someone else can provide some insight as to why this happens.)
Jesse might very well be right, I don’t have much experience within Unity. But of course in regular programming, you absolutely have to initialize your array elements.
How about with an interface, any idea how to get an array using a interface class to show up in inspector… wont let you use System.Serializable on an array