x


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?

more ▼

asked Sep 26, 2012 at 06:42 AM

Sophia Isabella gravatar image

Sophia Isabella
39 13 915 31

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

3 answers: sort voted first

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!

more ▼

answered Sep 26, 2012 at 10:56 AM

dscroggi gravatar image

dscroggi
291 2 2

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

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.

http://answers.unity3d.com/questions/318376/static-non-static.html

more ▼

answered Sep 26, 2012 at 06:56 AM

fafase gravatar image

fafase
23.4k 59 53 109

My game is a bit different in that it does not have a player object as such. Could I create a gameObject called StaticData in each scene and attach a static class to that. If I did that would it still have the same values from scene to scene?

Sep 26, 2012 at 07:10 AM Sophia Isabella

The game Object would be a new one but the data would be the same ones. It would be a new object linked to the same data.

Sep 26, 2012 at 07:17 AM fafase

@fafase - What about the script thing that dscroggi is suggesting. Have you needed that before?

Sep 26, 2012 at 10:06 AM Sophia Isabella
(comments are locked)
10|3000 characters needed characters left

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

answered Sep 26, 2012 at 07:34 AM

dscroggi gravatar image

dscroggi
291 2 2

@dscroggi - Can you explain a bit more? It's a method called DontDestroyOnLoad(this)?

Sep 26, 2012 at 10:05 AM Sophia Isabella

when you start a new scene, all objects from the previous scene are destroyed. This function tells to keep the variable when loading a new scene. Could be a solution. My issue is that if for some reason the object gets destroyed within the game, you would lose the data. The function keeps on load but I do not know if it preserves from Destroy. http://docs.unity3d.com/Documentation/ScriptReference/Object.DontDestroyOnLoad.html

Sep 26, 2012 at 10:21 AM fafase

@dscroggi - Can you show me how the class would look that I would need to hold the static data. All I need to do is to hold one integer for score? Thanks

Sep 27, 2012 at 02:49 PM Sophia Isabella
(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:

x8957

asked: Sep 26, 2012 at 06:42 AM

Seen: 4477 times

Last Updated: Sep 27, 2012 at 02:49 PM