x


How to make a global variable in Unity?

im trying to make a global variable so other scripts can do stuff with it. How do i make a variable global?

edit using java

more ▼

asked Mar 05 '11 at 05:17 PM

NathanDaniels gravatar image

NathanDaniels
60 2 2 10

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

3 answers: sort voted first

I'm pretty sure this has already been answered but you can always put

static var

infront of something and that makes it accesible from other scripts.

more ▼

answered Mar 05 '11 at 05:20 PM

AngryOldMan gravatar image

AngryOldMan
2.7k 14 21 48

So simple but thank you!

Sep 26 '12 at 08:28 AM Nercoe
(comments are locked)
10|3000 characters needed characters left

You can use the "static" modifier. In C#, that would be

public static int myInt = 0;
more ▼

answered Mar 05 '11 at 05:20 PM

jashan gravatar image

jashan
11.1k 25 46 122

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

Use Singletons!

They're much better than using static.

Grab the singleton script above and simply use it as such:

public class MyClass : MonoBehaviour {
    void Awake () {
       Debug.Log(Manager.Instance.myGlobalVar);
    }
}

Manager.cs

public class Manager : Singleton<Manager> {
    public string myGlobalVar = "whatever";
}
more ▼

answered Sep 04 '13 at 12:15 AM

Cawas gravatar image

Cawas
2.1k 42 52 69

(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:

x1213
x155

asked: Mar 05 '11 at 05:17 PM

Seen: 14769 times

Last Updated: Sep 04 '13 at 12:23 AM