- Home /
project wide variables and constants
My project relies on a few scripts that need to be persistent throughout the whole life of the application, and their function is to basically hold the reference and values of gameobjects and states, so I was wondering:
is there a way to create project wide variables that you can access without
GetComponent and GameObject.Find?
I have just started thinking about the editor, but apart from the tutorials haven't seen much of them around:
do you think that it's possible to create an "editor class"(<= totally guessing here) that will exist
throughout the whole application, and which could replace my persistent scripts
in storing my variables, and that I could access without .Find?
My current scripts rely on dontdestroyonload and they work with it, but I am forced to perform many .Find and .GetComponent to get the references of my scene objects and variables (there's a lot of interaction in all scenes).
thanks
Answer by sven1994 · Aug 12, 2011 at 08:31 AM
You can have a class (doesn't have to derive from MonoBehaviour) with public static variables. You can then just access them from everywhere in your project with ClassName.variable, but I don't think you can assign static vars in the editor. Example:
class MyVariableStorage
{
public static int importantVar = 0;
}
class AnyScript : MonoBehaviour
{
private void Start()
{
Debug.Log(MyVariableStorage.importantVar);
}
}
Do remember though, when using statics, that they do not get reset when you load a level.
Thanks a lot for the suggestion, but static offers a different approach to what I'm trying to achieve, since I'm actually trying to have these variables exposed somewhere that I don't need to instantiate or create as a gameobject in my scenes (I have a few classes that 'dontdestroy', but of course they only exist if I come from the scene they're generated into).
It is perhaps possible to mix the static variable definition with an editor override or new editor class? lol my brain freezes at this point... could I possibly add a new 'project settings' submenu, like 'global objects', which could open an ad-hoc menu in the inspector?
Any of you has any experience on editor tweaking to know if that's a doable solution?
Singletons maybe?
Thing is, you can access statics from any script, even if they arent in the scene, usually i make a script with a load of statics in them if i want to save some data between scenechanges
Answer by synapsemassage · Aug 12, 2011 at 10:59 AM
public var myPublic : float = 0;//if no other value is assigned in editor it will reset static variable to 0
static var myStatic : float;
function Awake()
{
myStatic = myPublic;
}
thanks but this is still a bit far from what I'm asking, since to expose myPublic you need to attach the script to a gameobject in the scene.
Your answer
Follow this Question
Related Questions
Global Varible Problem 2 Answers
Global Variables with Prefabs/ Self-Naming Variables 2 Answers
Howto access global var from other script? 2 Answers
Newbie questions! (Variables and Saving) 2 Answers
How do you achieve variables and functions that are global between scenes? What is the BEST way? 1 Answer