Use of "Static" = Bad Practice? [Resolved]

Hello,
I have a gameobject in my main scene with a script attached. The script is similar to as follows:

public static GameController Instance;

public GameObject prefab1, prefab2, prefab3;

void Start()
{
    if (Instance == null)
    {
        Instance = this;
    }
}

When I access GameController’s prefab1/prefab2/prefab3 from a different script on a different gameobject, I call:

void CreateBullet()
{
    GameObject instance = Instantiate(GameController.Instance.prefab1) as GameObject;
}

Is this bad performance-wise? My game often changes scenes … is the garbage collector able to clean up old GameController Instances? Are there any other negative implications from coding this way? I have heard that many people opt to use Singletons instead - but are the performance gains that great?

Thank you.

No. In general, there is no performance difference between a static reference and any other kind of reference.

The garbage collector will clean up any GameController instance that no longer has any references to it. The static Instance variable counts as one such reference. But if that variable is not pointing at a particular instance of GameController, and nothing else is as well, it will be garbage collected.

Singletons like you have here are a bit inflexible. They create a dependency that the singleton instance must always be present in the scene, and if it is not, things break. That doesn’t mean they are objectively bad or that you shouldn’t use them. It is just a tradeoff of the singleton architecture.

Your code here is a simple singleton implementation.

1 Like

This is fine in my book but you’ll hear other developers screeching about how “singletons are a bad anti-pattern” blah blah blah. As long as you understand what you’re doing, I reckon it’s fine.

Also, it is customary to assign the Instance in Awake().

The reason is because if you have two scripts in the same scene and in their Start() call one of them tries to access that Instance, it might be set up, or it might not, 50% chance, and no way to reason about it. It might work for a while then fail.

Here is some timing diagram help:

If you understand the order of operation, you can reason about lifetimes of these singletons a lot better.

1 Like

ALSO, while we’re at it, here are some super-simple Singleton examples to take and modify:

Simple Unity3D Singleton (no predefined data):

Unity3D Singleton with Prefab used for predefined data:

These are pure-code solutions, do not put anything into any scene, just access it via .Instance!

If it is a GameManager, when the game is over, make a function in that singleton that Destroys itself so the next time you access it you get a fresh one, something like:

public void DestroyThyself()
{
   Destroy(gameObject);
   Instance = null;    // because destroy doesn't happen until end of frame
}

Thanks so much for the answers guys. It’s tremendously appreciated :slight_smile:

@Kurt-Dekker
Okay great! At a studio that I used to work at, the lead developer was screeching about singletons vs static references … which is why I thought I’d double check here haha. Thanks for all the extra information too

@PraetorBlue
Okay, thanks! We are very careful to ensure that there are Instances assigned, and we don’t run into issues any more.

1 Like