[Solved] Variable Initialization Question?

Hello All,

Just two quick question.

I am using Unity 4.6.1 for a little project and I noticed some odd behaviour today. I have initialized a bunch of variables in my script. And the Inspector doesn’t reflect the values that I have set and I don’t understand why? And secondly if I change the values in the script the Inspector doesn’t reflect these changes and continues running its merry way with the older values. Again I can’t understand why. The only way to change the variable is in the Inspector, or, remove the variable from the public list run the program then stop it and put the variable back to recognize the change - mightily annoying.

It doesn’t matter if the values are initialized within the class, but, outside of the Start() function or in the Start() function itself. Do I need to put these variables in the Awake() function?

So, can someone explain to me what is happening her or should I need to put these variables in the Awake() function.

Regards.

Hello,

Setting the public variables value in the script (outside of any methods) means you are giving the variable a default value. If you change this in the editor it will no longer be it’s default value because you changed it. If you want to only use the value in the script, you should make it private or assign it in the start method.

Goodluck with your progress!

Thank you for your reply it helped me understand my problem. I understand why making the vars private is a good idea but at the moment I need to know what the values are at runtime, so, having a visual record of what is happening is necessary for the time being the console is being used for other things at the moment like registering collisions and whatnot.

When everything is working I will change them to Private.

Regards.

You can change the inspector to debug mode. There you can see the values of private variables.

1 Like

Really!!! How do I do that?

There is an expandable menu at the top-right of the inspector.

1 Like

Oh, so there is! Never seen or read anything about this before - I must investigate :p.

This could be a life and time saver cheers :sunglasses:.

Regards.