- Home /
How to prevent script instances from overriding default global variables set in the script?
Without making all the variables private and then having to make tons of Get functions, and switch the inspector to debug mode to view values during gameplay. If I save a new value in a script, Unity should update all script instances. Especially if it's just one. How to make the scripts static somehow? It's gonna be one script instance per each script anyway (main player object with a few scripts here and there).
Answer by Ehren · Feb 25, 2010 at 08:55 PM
You could set the values of your member variables in Awake().
var foo : float;
function Awake(){ // Ignore inspector setting and use scripted value instead. foo = 4.0; }
Answer by Tetrad · May 16, 2010 at 05:54 AM
What you want are public member variables for script access but you don't want the editor to access them.
There's a built in way to do this, just set the attribute HideInInspector.
http://unity3d.com/support/documentation/ScriptReference/HideInInspector.html
For Javascript you'd just preface the variable with @HideInInspector. In C# you'd prefix it with [HideInInspector]. (Granted, in C# you'd probably use properties instead of public variables.)
Answer by Joe Wreschnig · Feb 01, 2010 at 11:53 PM
Why set/change a value in the script at all? Just set it only in the Inspector.
Answer by cregox · Feb 25, 2010 at 08:03 PM
The question body is too confuse. So I'll answer just the title.
In csharp
, simply use internal
instead of public
:
public bool aPublicBool = true; // you may change this in the inspector
internal string likeAPublic = "ok"; // can only be seen on the Debug inspector
old answer
If you set the value for a variable on the script and outside any function, those variables that will be available on, that's the default value. When you add the script to some object, it will use those values.
When you do that, the instanced script will be saved in the project with those values. So, if you later change the script default values, the old ones will persist.
If I understand right, you want to override this setting. I highly doubt there's any simple way to do that.
But you can set all variables to private, save the script so the inspector will remove the variables, then set it back and save the script again. No need to create get functions at all.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to make tab in editor near the top? 1 Answer
Are any UnityEditor Classes available at runtime (outside of the editor)? 1 Answer
How can "bounds" for script variables be defined for use in the Inspector? 2 Answers
Textmate - features: shortscuts for c#, javascript, shaderlab? 1 Answer