Learning C# 7 By Developing Games with Unity 2017(Third Edition)
上QQ阅读APP看书,第一时间看更新

Watching for a possible gotcha when using public variables

Unity gives us great flexibility with editing or reading public variables in the Inspector tab. You will be using public variables most of the time. Now, I want to make you aware of something that might give you a headache sometimes.

All public variable values are overridden by the Unity Inspector tab.

Let's look back at line 6; we had assigned our variable a value of 9. This value will be copied to the Unity Inspector. From now on, the value from Inspector is taken into account and not the value in the script, even if you change it. Therefore, be careful as this is very easy to forget.

In the Inspector panel, try changing the value of myNumber to some other value, even a negative value. Notice the change in the answer in the Console tab.