Avoid using constructors do not initialize any variables in the constructor, use awake or start to achieve this. Even in edit mode, unity automatically calls constructors, which usually happens after a script is compiled, because the constructor needs to be called to orient the default value of a script. The constructor is called not only at unexpected times, but also as a preset or inactive game object. ”
The Monobehaviour has two lifecycles, one for the C # object, and one for the component cycle.
The constructor represents the first one, and awake represents the second.
Editor's code and script code in the same AppDomain, the object's life cycle behaves differently than the player environment. For example, the number and timing of constructor calls in editor is not the same as the build-out game, so it is not easy to guarantee correctness.
Another key reason is that constructors are executed on loading threads inside unity, one cannot use the unity API, and the other is the need to consider synchronization issues, which is more difficult to ensure.
So it's nothing. Constructors, readonly fields, and so on, can only be initialized in the construction phase, try not to use, design needs to ensure that the initialization code as simple as possible.
Right and then
Links: https://www.zhihu.com/question/24701230/answer/50546298
Source: Know
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.
Constructors in Unity