Skip to content

Instantly share code, notes, and snippets.

@luisartg
Created March 31, 2019 04:04
Show Gist options
  • Save luisartg/99cd1ae7c504087ade39df8fb3ab1780 to your computer and use it in GitHub Desktop.
Save luisartg/99cd1ae7c504087ade39df8fb3ab1780 to your computer and use it in GitHub Desktop.
Singleton Game Session
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameSession : MonoBehaviour
{
[SerializeField] int score = 0;
private void Awake()
{
SetupSingleton();
}
private void SetupSingleton()
{
int gameSessionsCount = FindObjectsOfType<GameSession>().Length;
if (gameSessionsCount > 1)
{
Destroy(gameObject);
}
else
{
DontDestroyOnLoad(gameObject);
}
}
public int GetScore()
{
Debug.Log("My score is " + score);
return score;
}
public void AddToScore(int points)
{
score += points;
}
public void ResetGame()
{
Destroy(gameObject);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment