Why does Inspector take precedence over code files for variables?

Let’s say I change a variable in a C# file. Nothing happens. Then I realize it’s because the variable is showing up (unchanged) in the Inspector under the script component variables. How do I avoid this?

Thanks

You can’t avoid it, and Unity would break if you could. When you put a script on an object, it’s an instance. Being an instance, that means that every object has its own copy of the public variables.

Imagine what would happen if changing the variables in a script somehow overrode the values in the inspector: Let’s say you have a complicated script, and you make a scene with several instances of objects with this script, and you spend hours tweaking all the public variables on the objects to get the effects just right. Then you decide to make a change somewhere in the script and save it. Bam, all of your carefully tweaked public variables are reset, and all your work is gone!

Obviously, that scenario is completely unworkable, and it’s the reason values must always come from the inspector, never from the script (aside from when the script is first assigned to an object, of course). If you want variables to only take their values from the script, not the inspector, then don’t make them public variables.

–Eric

A quick way to force all instances of a variable in the scene to change the the scripts value is to change a public into a private (or non-serialized), save, let it compile, then change it back to public - something I’ve done on a few occasions.

But with multi-object editing, this is kind of obsolete, just select all the objects with the script attatched, and you can change variables on all of them