- Home /
script default variable value behaviour
Hello. I have a simple script with few variables, like:
var value1 : int = 99;
var value2 : String = "hello world";
function Start () {}
function Update () {}
I put it on some object and change the script values, like:
var value1 : int = 101;
var value2 : String = "unity";
Then I open inspector and see previous values (99, "hello world").
If I remember correctly the old behaviour would override variables in inspector to the newest (default) values in script if you didn't change them by hand (highlights as bold).
Currently it doesn't even highlight them as bold until I make a prefab of the object.
So this means I have to reset manually all of my gameobjects which use this script to apply new default values.
Ex. I have 10 monsters based on MonsterSoldier script who has speed and damage variables. Then I change my mind because monsters are too weak and change the script damage to a bigger value. I except this would be applied to all mosters, but no - it's not... it doesn't applied at all.
Is it just me or this is a standard behaviour in unity and all previous releases? I can't remember I ever stumbled on this before.
Answer by fafase · Jun 15, 2014 at 04:35 PM
Std behaviour in Unity. Public variables get the value only on first drag. Then you modify via inspector. If you want to have to get the new value from the script you need to press Reset on the script but that will reset all variables to script values.
Or use a private variable.
Answer by robertbu · Jun 15, 2014 at 04:35 PM
This is standard behavior. If the variable is public, then initialization code is only used at the time the script is attached. If you want to set the values in code rather than in the Inspector, make the variables private:
private var value1 : int = 101;