Constructors in Unity

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.