In lecture 136, the instructor creates two new serialized variables and assigns them initial values. At no point in the lecture or the next do I see those initial values changed.
Unity can’t start with any of its serialized variables with unknown values. If you hit “Play” and Unity has variables with unknown values, it will try to take an initial value assigned by code from it’s initialization. If there is no value assigned during initialization, then Unity will instead assign a default value (something like 0, “”, or null). But once it has that value, it will remember it.
If you then go back and change the initial value in your code, it will not change Unity’s remembered value. Since Unity already remembers its value from before, it does not query the code again. This is an intentional “helpful” feature that allows you to set values permanently from your inspector, which many people like to do – it’s usually the reason why people serialize those values to begin with.
In the next lecture, the instructors goes into Unity and hits “play”. At that point, any serialized variables that Unity remembers a value for will overwrite their coded initial values with Unity’s value. If Unity does not have a value, it will use the initial value from code and “remember” that. Otherwise, it will just assign a default and remember it. No matter what, once you hit play, Unity is remembering that value.
Here’s a little thing that used to get me a lot. I would declare a value, but not assign it. For whatever reason, I’d hit play. Then I’d go back at some point and try to assign an initial value to the variable. Then I’d wonder went wrong. Because by this point, Unity will have already assigned a neutral default value to my un-initialized serialized variables. For example, my maxSpeed variable would be assigned 0 by Unity. But I didn’t realize that, and it didn’t show up in my code, so later when I changed [SerializedField] private int MaxSpeed;
to [SerializeField] private int MaxSpeed = 600;
I would wonder what was going wrong. This is because in Unity, maxSpeed was 0. Because it had no initial value when I hit Play, so Unity assigned it one. And that value is still in the inspector, even though my code has since been changed.
Again, if you don’t want Unity controlling your variable, just don’t serialize it. But when you serialize it, you are surrendering some control to Untiy.
Once the game is started and methods are being called and such, then the values can be changed (temporarily, during runtime). Your code will still work normally. It’s only initialization that gets overwritten. Once you stop playing, all values will return to Unity’s remembered values. This is why you can set the value in Awake() and it will override Unity’s remembered value, at least while playing.
Let’s look at another scenario real quick. Let’s say I go back to my code and I make a new variable called minSpeed, and I set it to 10 as [SerializeField] private int minSpeed = 10;
. In this case, when I go back to Unity and hit Play, it sees the serialized field “minSpeed” but has no value for it, so it looks for an initial value in code. this time, I have one – it’s 10. So Unity will see the 10, and remember it.
This is basically what the instructor did in the lecture. He created new variables and set their initial value by code. Then he went to Unity and Unity read the initial values there. there was no need to set it in Unity. However, once Unity has that value of 10, I can’t just say “Oh, that’s not fast enough, I’ll go back and change it to 20.” This is because Unity is now controlling that initial value, not my code. If I change it to 20, Unity will use “10” when I go to hit Play because that’s the value in Unity’s inspector now. Unless I change it within Unity, or change it in one of my methods (after initialization).
I hope that clears things up more than it confuses things. I’m worried that I’m making it seem more complicated than it is.