How can I have a static class I can access from any place in my game?

If I have the following code:

static class SceneData
{
   static int width=100;
   static int score;
   static GameObject soundManager;
}

Can I use this to keep score that’s available in all my scenes? If so then do I have to make an empty gameobject in each scene and then just attach the script to that or is there more I have to do?

Sure! I am using the same methods in my own project right now.

First let me say that a guy named Petey has a youtube channel called BurgZergArcade that has some really awesome Unity3d C# programming videos!

That channel is here:
http://www.youtube.com/user/BurgZergArcade

Anyway what I usually do that I learned from Petey is make a new empty gameobject and name it GameMaster, then make a C# script named GameMaster.cs.

There’s at least two ways to do this. Here’s the first way:

using UnityEngine;

public class GameMaster : MonoBehaviour 
{
	public static int score;
	
	void Awake()
	{
		DontDestroyOnLoad(this);
	}
}

This is the quick and easy way to access variables like ‘score’ from any class. To do this just call GameMaster.score from anywhere. The problem with this method is that it only works well for simple data types like int. Most of the time we want to drag and drop in Assets via the inspector, and this isn’t allowed with static variables.

In this case we create what’s called a singleton. This is something Leepo from M2H uses in his multiplayer tutorials. We create a regular class and then make one single static instance of the whole class. This is the second way:

using UnityEngine;

public class GameMaster : MonoBehaviour 
{
	public static GameMaster GM;
	
	public GameObject soundManager;
	public int score;
	
	void Awake()
	{
		if(GM != null)
			GameObject.Destroy(GM);
		else
			GM = this;
		
		DontDestroyOnLoad(this);
	}
}

The beauty of using the singleton is that it can use the inspector to assign public assets and can still be used anywhere like a static class, even the functions will work from anywhere. We’d call the score for example by using GameMaster.GM.score

Of course there are many other ways to accomplish this, but this is how I like to do it. Hope that helps!

Yes you can do this, but you need to specify everything as public.

public static class SceneData
{
    public static int width = 100;
    public static int score;
    public static GameObject soundManager;
}

Otherwise they default to private and your class will be useless.

Now you’ll be able to use SceneData.width anywhere.

The other answers offer ways of having a class that inherits from MonoBehaviour which you can access from anywhere, but it doesn’t look like you actually want any MonoBehaviour functionality, so a simple static class will work better.

The way I do it is that I have a normal variable (I call it temp) that I use in the scene and a static variable (static) that I use for the whole game.

The reason is, if you use the static var for your point and the guy loses, when he starts again, he still has the point from previous run.

You could use temp and if the guy wins the level then

static += temp;

If the guy loses

temp= 0;

Temp is lost when the new level is loaded, static remains.
You can attach the static var to your player object. The static var will have a lifetime of the whole game and you can access it anywhere with ClassName.staticVar.

In case you would not know much about static yet, it has been subject for a lot of controversy as beginners tend to think that the easiness of access is a good feature. Beware of that.

Throw this utility script on any game object and it will not get destroyed when scenes change. Make sure that game object has your static data on it. Btw as far as I’m concerned, static data is faster to access than instanced data, so you’re on the right track.

using UnityEngine;

public class KeepOnLoad : MonoBehaviour 
{
	void Awake () {
		DontDestroyOnLoad(this);
	}
}

just declerate public static class and keep it in asset folder. public static class may look like this

public static class TheStaticClass 
    {
         public static int uselessNumber = 33
    }

you can acess uselessNumber from any place in any code by

TheStaticClass.uselessNumber