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

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.

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";
}

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

public static int myInt = 0;

Hi,

This tutorial explains very clearly and simply how to create global variables using Singletons!

Hope it helps!

Written a blog related to the question which gives the indepth information related to varible scope. link:Variable scope in Unity

Use My Method!

As my method is a mutation of static variables, and Singletons. Slowly refined and forged within the mountains of Coding Space, to bring the best possible coding structure to all coders! Whether you’re brand new to coding, or have been coding for years and think you know it all, this code structure will make you want to sit down and eat some cake… Because it’s as easy, as performant, and as sleek as Pi .