Let's say we have a MonoBehavior class that has a custom variable declared as follows:
using UnityEngine;
public class TestScript : MonoBehaviour
{
public static SimpleClass ClassInstance = new SimpleClass(10f);
}
"SimpleClass" is a simple C# class with a variable and a constructor:
public class SimpleClass
{
public float Value;
public SimpleClass(float value)
{
// When will this code execute?
Value = value;
}
}
I've spend whole evening trying to get when Unity actually calls constructor code in its execution order (if it even related to it).
When you create a struct it's declared instantly (as visible in editor), but this is not the case when class instance is produced. (unless it's List or Array)
In this example "ClassInstance" gets created even if MonoBehavior is disabled or put on inactive GameObject - so it's not "Start" or "Awake" loop too.