x


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).

more ▼

asked Feb 01 '10 at 07:36 PM

Micha Lewtak 2 gravatar image

Micha Lewtak 2
183 7 7 9

I'm confused. Can you provide an example?

Feb 24 '10 at 12:45 AM Cawas
(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

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;
}
more ▼

answered Feb 25 '10 at 08:55 PM

Ehren gravatar image

Ehren
4.2k 34 43 77

d'oh. Yeah, and then, after all I said, there's that! :P

Feb 26 '10 at 01:09 PM Cawas
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Feb 25 '10 at 08:03 PM

Cawas gravatar image

Cawas
1.3k 31 38 54

(comments are locked)
10|3000 characters needed characters left

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.)

more ▼

answered May 16 '10 at 05:54 AM

Tetrad gravatar image

Tetrad
7.2k 27 37 89

(comments are locked)
10|3000 characters needed characters left

Why set/change a value in the script at all? Just set it only in the Inspector.

more ▼

answered Feb 01 '10 at 11:53 PM

Joe Wreschnig gravatar image

Joe Wreschnig
75 3 3 8

Because. It's faster for me.

Feb 02 '10 at 05:33 PM Micha Lewtak 2
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5088
x1677
x108

asked: Feb 01 '10 at 07:36 PM

Seen: 1942 times

Last Updated: Feb 01 '10 at 09:18 PM