An Array of Classes with variables in C#

Well… I’m not that familiar with C# in Unity… So I’m a bit confused by this…

In Javascript, you can create a class, give it some public variables, and then make a public array with the class, so that you can have an array of classes to modify in the editor. Like…

class thePerson{
var theName:String;
var theAge:String;
}

Then after that, in another script, you have

var thePeople:thePerson[];

Then in the editor, it will have a list of thePerson classes with theName and theAge of each thePerson modifiable…

How do you do this in C#?

The issue here is that in UnityScript, custom classes are declared serializable implicity, wheras in C# you need to specify it using the ‘System.Serializable’ attribute:

[System.Serializable]
class ThePerson {
    // theVariables
}

Then, so long as fields are declared public, you should have no problems.

public ThePerson[] thePeople;

Unity will expose any “public” value to the inspector. In UnityScript, variables are public unless you specify that they are private; in C#, variables are private unless you specify that they are public.

//UnityScript
var foo : int;         //seen in inspector
private var foo : int; //NOT seen in inspector

//C#
public int foo; //seen in inspector
int foo;        //NOT seen in inspector

If you want to learn more, Unity’s script reference has some pretty detailed information at the SerializeField attribute page.