[Unity-19] The assignment process of global variables in unity

Source: Internet
Author: User

Friends who develop with unity may encounter the problem that I have defined a public global variable in the script and assigned a value, but it is not the value of the variable after running, so let's explain why.

The code is as follows:

public class Test:monobehaviour {public    int a = ten;    void Awake ()    {        a =;    }    void Start () {        a = 30;}}
because it is a public type, variable A is displayed in the Unity panel, such as:

So what is the order of assignment for this variable a?

1. First the value of a is the value assigned at the time of definition 10.

2. Then the value of a is replaced by 15 in the Unity panel.

3. Then the value of a is replaced by 20 in awake.

4. Then the value of a is replaced by 30 in start.


See this result to believe that everyone knows why, the public variables to initialize is best put into start, and do not believe the effect of the definition. Of course you can also use [Hideininspector] to make the variable not appear in the Unity panel.


[Unity-19] The assignment process of global variables in unity

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.