I have attached a script to a Unity game object. The script contains various public properties, including some of my own classes. Like in the following simplified code, where the TestMonoBehaviorClass
is attached to a game object and the TestClass
' TestString
is shown in the inspector.
public class TestMonoBehaviorClass : MonoBehaviour
{
public TestClass Test;
}
[System.Serializable]
public class TestClass
{
public string TestString;
public TestClass ()
{
Debug.Log ("TestClass creator called.");
this.TestString = "Reset String";
}
}
I would expect that the constructor of the TestClass
(Edit: NOT the one derived from MonoBehavior) is called once, when I attach the script to the game object. But it is called four times if I run the program in the Unity editor and then stop the program. Seven times if I have the script attached to two game objects. At least I see the output of the Debug.Log in the console that many times.
Still, if I change the content of the TestString
property in the editor, the content I enter manually is NOT overwritten!
Why is the constructor called that often? When is it called in Unity's execution order (Unity's execution order of event functions)? Can I kind of ignore the call, or do I have to add special handling into my constructor? So far I didn't see any actual problem or side-effect.
Edit: It seems that only constructors without parameters are called. If I only have constructors with parameters, then none is called.