Skip to content

Instantly share code, notes, and snippets.

@eduardonunesp
Created September 3, 2019 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eduardonunesp/7f3aaa2bceb8daaba7bfb84f44ebcdab to your computer and use it in GitHub Desktop.
Save eduardonunesp/7f3aaa2bceb8daaba7bfb84f44ebcdab to your computer and use it in GitHub Desktop.
Singleton Pattern
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
public static GameManager Instance { get { return _instance; } }
private static GameManager _instance;
void Awake()
{
//Check if instance already exists
if (_instance == null)
{
//if not, set instance to this
_instance = this;
//If instance already exists and it's not this:
}
else if (_instance != this)
{
//Then destroy this. This enforces our singleton pattern, meaning there can only ever be one instance of a GameManager.
Destroy(gameObject);
}
//Sets this to not be destroyed when reloading scene
DontDestroyOnLoad(gameObject);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment