- Home /
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#?
Answer by syclamoth · May 13, 2012 at 09:17 AM
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;
Answer by rutter · May 13, 2012 at 09:03 AM
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.
Thanks for the help! The SerializeField thing worked! And I much appreciate that little lesson on the differences between UnityScript and C# :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613071428im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I delete or empty an array of custom structs during edit mode? 1 Answer
How to access elements in an array through a SerializedProperty? 1 Answer
Initialising List array for use in a custom Editor 1 Answer
Insert new custom class element with _default_ values to a SerializedProperty array? 1 Answer
How to Save an multidimensional array trough editor script 1 Answer