x


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

more ▼

asked Aug 12 '11 at 07:39 AM

roamcel gravatar image

roamcel
1.2k 37 40 44

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

2 answers: sort voted first

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

answered Aug 12 '11 at 08:31 AM

sven1994 gravatar image

sven1994
291 2 3 10

Do remember though, when using statics, that they do not get reset when you load a level.

Aug 12 '11 at 08:35 AM Kacer

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?

Aug 12 '11 at 09:52 AM roamcel

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

Aug 12 '11 at 09:57 AM Kacer
(comments are locked)
10|3000 characters needed characters left
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;
}
more ▼

answered Aug 12 '11 at 10:59 AM

synapsemassage gravatar image

synapsemassage
441 2 5 17

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.

Aug 12 '11 at 11:25 AM roamcel
(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:

x826
x269
x123
x15
x5

asked: Aug 12 '11 at 07:39 AM

Seen: 1322 times

Last Updated: Aug 12 '11 at 11:25 AM