[System.Serializable]
public class Test
{
public int p;
public float v;
public Color c;
public Test()
{
p = 2;
v = 4.4f;
c = Color.yellow;
}
}
public class Example : MonoBehaviour
{
public Test test;
public Test[] testArray;
}
The result is that test and testArray variables are editable in the inspector. test is properly started with initial value in the constructor of Test class. But for testArray, when I edit array size in the inspector, the initial value of those variables are always system default. (p = 0, v = 0, c = Color.black) It isn't used the initial value in the constructor. I've already tried to define value in the declaration part but it doesn't help. Is there a way to work around this problem? This is just a test code. I intend to have many more editable variables in the class. Without initial value, it would be a pain to edit all of those variables again and again when increasing the array in the inspector.
public class Example : MonoBehaviour
{
public Test test;
public Test[] testArray;
public Example()
{
Test a = new Test();
a.p = 5;
Test b = new Test();
b.p = 100;
testArray = new Test[] { a, b }; //your array will start with a and b in the inspector by default
}
}
Note: using the constructor for monobehaviours is usually a bad idea, but this is one place that it'll work well, as it'll create the array before the deserializer adds in any values. It will however mean that the array is assigned twice at runtime - You can however comment/remove the code before making a final build though
Old question, but I had the problem myself and came across this while searching. @mike-3 has an okay solution, but it doesn’t work when new items are added to the list.
Eventually I figured out something that works pretty well. It uses the wonderful OnValidate() message.
[Serializable]
class Test
{
public float size;
public string food;
public Test()
{
// This ensures the defaults are set in normal non-array situations.
SetDefaults();
}
public void SetDefaults()
{
size = 10;
food = "Pizza";
}
}
class TestBehaviour : MonoBehaviour
{
public Test[] tests;
// OnValidate gets called by the editor whenever a value is changed. Awesome!
private void OnValidate()
{
foreach (Test test in tests)
test.SetDefaults();
}
}
No. Array values must be initialized manually. If you have `Test[]`, the type is actually "Array of Tests", which is not the same as a "Test". Anytime you have an array, each cell has to be initialized independently. Unity handles this for you because of the inspector, so it just assigns the default/null values to the type. If you want to be able to control the values, or assign them via the constructor, you need to do something like this:
public Test[] arr;
void Start()
{
arr = new Test[10];
foreach(Test t in arr)
{
t = new Test();
}
}
That will initialize all of the values in the array. Unity does not do this for you, as far as I know.
This is how you do it. When Unity creates the ‘myStrings’ property for viewing in the inspector, it will call the ArrayInitializer() constructor, assigning the default values you pass as parameter:
public MyClass : MonoBehavior
{
[System.Serializable]
public class ArrayInitializer
{
public string[] values;
public ArrayInitializer(string[] defaults) { values = defaults; }
}
public ArrayInitializer myStrings = new ArrayInitializer(new string[]{"a", "b", "c"});
}