Arrays of references?

Hello,
I am trying to make it so that my cs scripts will use an array of structured reference variables, but I don’t want to have to end up setting the size for each individual array in my script box, so that is why I would like to use a structure of arrays instead of using a group of arrays next to each other. How would I do that? I tried storing the variables in a class and then using the class as an array, but when I go to the component box of my script, I then see a drop-down list looking for groups of objects that uses the class that I use, in which I have created none so far. I don’t want to place an entire class into my reference variable. I just want to set the size of 1 variable that will give me access to multiple vatiables within that structure. Is there a way to do this?

Thank you,
Michael S. Lowe

I didn’t quite follow that, but you can make a custom class type work with Unity’s built-in serialization system by using the Serializable attribute. (Note that this won’t work with structs.) Sometimes you can run into cases that Unity’s serialization system won’t handle, but generally speaking, you should be able to create either an array of class types or a class that holds multiple arrays, as needed. (If the objective is to avoid having to set multiple arrays to the same size, it seems an array of a class type would be what you’re looking for.)

What I meant is that I would like multiple Behaviours inside an array where if I go to my script component box and enter the array’s size, all the variables inside that array will show up as many times as the size of my array is. Right now, I have each variable stored as a separate array, and that means for each variable, I would have to re-enter the array’s size per variable and hope that all sizes are equal without any typeos. It would make more sense if I could enter the size one time and have my list stored in an array.

Define a serializable class to hold the data:

[System.Serializable]
public class YourType
{
int i;
float f;
Color c;

}

public class YourComponent: MonoBehaviour
{
public YourType[ ] yourArray;

Right, that was what I was getting at when I said you could make your own class type serializable (basically what Berk showed in his post).

@ Berk: I’m not sure those fields will serialize if they’re not public or marked with the SerializeField attribute.