- Home /
Per-application versus per-level editor values
Hi fellow Unity developers,
When I attach a script to a GameObject in the hierarchy, its public members become visible in the Unity editor. This is a great feature, because it means everyone can tune values when the game is actually running. And you can just drag assets to make them available in scripts.
My problem is that the hierarchy is part of a scene, i.e. the (tuned) values are specific to that level only. After finding the right value, you have to copy them to all the other levels. For some values, this is fine. You want to set them differently for each level. However, some values are essentially game-global.
So my question is: is there a simple way to get per-game (or per-application) parameters in the Unity editor? I can do it by creating an empty 'common' level with just a dummy cube with a script attached that has my game-global values in a class. When I start a normal level, I use Application.LoadLevelAdditive(), which gives me access to the values in the class (in the next frame). This works, but it seems a bit too complicated.
Thanks,
Mattijs
Answer by Loius · Dec 06, 2010 at 04:23 PM
The solution is to make them prefabs and, when you need to change all instances, use the GameObject -> Apply Changes to Prefab menu option.
Alternately, use private vars in your scripts if they're not going to change, and / or give them a default value.
private var potatoes = 7;
This way they can't be accidentally changed in the editor. If you give a default value to a non-private var, keep in mind that any changes in the inspector will overwrite it, and if you change the default value later, it won't update the values on the existing objects.
Right, so I should split my tuning values into two scripts. The values that can be different per level go into script 1, and for the game-global values, I can create prefabs as you suggested. of course I can make stuff private, but being able to change values from the editor while the game is running is really useful...
Oh, and I forgot: thanks for answering my question:) Greatly appreciated!
Answer by Anton Petrov · Dec 06, 2010 at 04:20 PM
Call DontDestroyOnLoad( this.gameObject ) method in your global script's Start() method. And it will stay forever.
Thanks for the reply. I don't think this helps, however. The problem is that multiple instances of the script are created, one per level. For some of the values in the script, this is fine (e.g. the amount of money when the level starts). For others (e.g. gravity in the game), it doesn't work. If I change the script value, I have to change it for each level where I have attached the script to a gameobject.
I should add that the problem is not that I need the same values in level 2 after playing level1. We directly start different levels from the editor at the moment.
Your answer
Follow this Question
Related Questions
Values set by [ExecuteInEditMode] script revert back in play mode. 2 Answers
Saving dictionary from editor mode in play mode 1 Answer
Editor Script - Multi-Editing Value Increase/Decrease Issue 1 Answer
Setting variables in public class to values from editor script 3 Answers
How can I edit model import settings from a script? 1 Answer