Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have a GameObject in Unity, which I've attached this script to, in order to make it function as a Static Singleton :

using UnityEngine;
using System.Collections;

public class SceneStatics : MonoBehaviour {


    private static SceneStatics instance;

    public static SceneStatics Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new GameObject ("SceneStatics").AddComponent<SceneStatics> ();
                DontDestroyOnLoad(instance);
            }

            return instance;
        }
    }

    public void OnApplicationQuit ()
    {
        instance = null;
    }
}

I have two other scripts that I'd like to be able to access from scene to scene. What's the proper way to do that?

Do I attach my Inventory and Room scripts to the same game object as the SceneStatics? If so, how do I access them? Can I go from SceneStatics.Instance to one of the other scripts directly via GetComponent?

Alternatively, what happens if I add two child gameObjects (each containing one of the other scripts) to the SceneStatics?

Or is the intended use that I simply make any other static items I need properties of the SceneStatics class? (I.e., add in a Public static Inventory and Public static Room attributes)?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

A really short way of ensuring there's only one instance of a script in the scene is to get it to destroy itself on creation if others are found.

public class UIManager : MonoBehaviour {

    void Awake()
    {
        // hyper-advanced singleton implementation
        if (FindObjectsOfType(typeof(UIManager)).Length > 1)
        {
            Debug.Log("UIM: Already found instance of script in scene; destroying.");
            DestroyImmediate(gameObject);
        }
    }
}

Does your class absolutely have to extend MonoBehaviour? If not, you might as well make it a static class.

share|improve this answer
    
Thanks, that's a good idea. Static class makes the most sense. –  Raven Dreamer Jul 25 '12 at 2:17

Personally I think singletons are an abomination and I don't recommend them. But you can implement them however you like. There's no such thing as a 'proper' way.

If you only want 1 of something, just create a single instance. You don't need a code straitjacket to stop you adding more, and you'll probably find a use for more later. So why bother?

Do I attach my Inventory and Room scripts to the same game object as the SceneStatics?

Nobody can answer that without knowing what those scripts do or what the implications of such an attachment are.

Can I go from SceneStatics.Instance to one of the other scripts directly via GetComponent?

Yes, certainly. If a GameObject has multiple components then you can easily find one from the other.

What happens if I add two child gameObjects (each containing one of the other scripts) to the SceneStatics?

Nothing happens. You now have 3 game objects. To access one from the other will require a little more than a simple GetComponent, but that's all.

Is the intended use that I simply make any other static items I need properties of the SceneStatics class?

There's no intention here, just many different ways of reaching a goal. I try to avoid statics in Unity as (a) they're generally just globals in disguise, and (b) you lose the potential benefit of being able to use the Inspector to edit the values. Instead I'll just have one GameObject with one instance of the component and will create it in the scene that needs it. In the very rare occasions I need something to persist throughout I just have 1 instance in the first scene and use DontDestroyOnLoad.

share|improve this answer
    
Well, my intent is to create global classes I can access from wherever. My precursory googling indicated that Static GameObjects were the way to go. Is there a better way to handle global and intra-scene logic? –  Raven Dreamer Jul 24 '12 at 22:32
    
Globals are generally considered a bad idea in programming, though to explain why would be beyond the scope of this answer. If you feel you need this functionality then statics give you that. –  Kylotan Jul 24 '12 at 22:43
    
Globals are often misused; they are not inherently bad. I have a suite of singleton controller classes that need to persist between scenes and be easily accessed by GameObjects. Do you suggest something other than a global and/or static solution? I asked this question because I'm trying to find the best way forward, not because I'm dead set on using statics. –  Raven Dreamer Jul 24 '12 at 23:33
    
I'm afraid I would disagree and say that a global variable or object is inherently bad. But that's not important here. My ideal solution for components that must outlast a single scene in Unity is to attach them to an object in the first scene and use DontDestroyOnLoad. –  Kylotan Jul 25 '12 at 0:46
    
The code structure I'm using these days avoids singletons, but I'm not sure if I'm using 'statics' in the sense that you guys are it. I do have globally accessible objects inserted into static variables, but which objects are inserted into those variables is controlled by an initialization object that swaps in different modules for different platforms. –  jhocking Aug 20 at 15:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.