C# : how can I create and access from the GUI an array or list of custom struct

hey guys

very simple question…
basically this is working because I use some unity type I guess :

public float[] m_EntitiesTEST = new float[4];
public Vector3[] m_EntitiesTEST = new Vector3[4];

I can access and setup the values directly from the GUI

But when I try to use my own class then I can’t get anything in the GUI !?
I guess I must be missing something pretty obvious

public  struct TEST
{
	public float a;
}
public TEST[] m_EntitiesTEST = new TEST[4];

any ideas don’t hesitate
thanks in advance
ox

http://unity3d.com/support/documentation/Manual/Using%20The%20Inspector.html

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.

Unity Manual page for Serializable attribute:

Very handy. After a bit of testing I haven’t managed to get it to work with structs, but it works fine for classes.

Incorrect. A struct is a value type.

My bad, I always seem to get dyslexic with those terms. My brain sees value and thinks “more valuable”. Stupid brain.

thanks guys !
using a class with [System.Serializable] actually worked !
cheers

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.

Well, if you just do the following:

myClass[] myObjects = new myClass[4];

You’ve created an empty array of myClass objects. They are probably set to zero because the objects in the array haven’t been created.

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.)

Don’t you have to call the class to initialize it?

GameEntity entities = new GameEntity[4];
entities[0] = GameEntity(); //???

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.

Yeah (although you’d use ‘new’ there in C#). However, we’re talking about arrays created via the inspector, which are handled differently.

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