- Home /
Do I have to change the value of a pubilic variable in the script even if I have already changed the value in the inspector variable?
I have attached a script to a game object in order to move it. A public variable of type Vector2 is defined to set the speed and values 50 and 0 are given. Now I have a field to set the speed in the inspector. If I change the value from the inspector instead of editing the script, Do I have to change the value in the script when I am going to build the project?
Thank you
Answer by ankush_Kushwaha · Jan 12, 2014 at 01:37 PM
No, You don't need to change it in your script. Unity3d prefer the value given in inspector.
Answer by frarees · Jan 12, 2014 at 01:09 PM
The values set in the inspector will always prevail to the directly defined in your script. That means, that if you do:
public Vector2 myVec = new Vector2 (50f, 0f);
That value will be overridden by the one found at inspector (that's because of Unity's internal deserialization). However, if you set that value on the proper Unity callback e.g. Awake
, Start
, OnEnable
... the values found on those methods will be the final values to be used. AFAIK Unity deserialization occurs between script's constructor and OnEnable
callback.
Another point to note is that if the script is attached to another object, that other object will start off with the default values as defined in the script.
Answer by Owen-Reynolds · Jan 13, 2014 at 12:13 AM
As per the KellyThomas comment above, the value in your script should be the "normal" value for that type of object. If you will only ever make one (like all bullets in the game are identical,) it won't matter.
Say you will be making lots of monsters, and 8 is a good walk speed. Use 8 in the script. Whenever you make a new type of monster, it starts with 8 in the Inspector. Suppose you want a slower monster -- you can easily see the 8 it starts with, and know that 4 is good for a "slow" monster.
If there is no normal value, like for monster hit points, can use something stupid in the script like -999, 1 or 99999. That way, when you make a new monster and test it, you'll see right away that you forgot to enter the hitpoints.
Your answer
Follow this Question
Related Questions
How to expose member variables to the inspector in C#? 1 Answer
View script variables within an array of scripts in Inspector 1 Answer
Inspector Drop Down for Variables 0 Answers
Is there a way to extend the amount of values for inspector? 0 Answers
Show multiple attached scripts variables in one script in the inspector 0 Answers