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