Created
March 31, 2019 04:04
-
-
Save luisartg/99cd1ae7c504087ade39df8fb3ab1780 to your computer and use it in GitHub Desktop.
Singleton Game Session
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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