Skip to content

Instantly share code, notes, and snippets.

@kurtdekker
Last active March 19, 2023 08:36
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save kurtdekker/50faa0d78cd978375b2fe465d55b282b to your computer and use it in GitHub Desktop.
//
// @kurtdekker
//
// ULTRA-simple fully-static GameManager for simple arcade-style games.
//
// NOTE: you DO NOT place this file into any scene! It is a pure static data container.
//
// Usage:
// - at start of game call GM.InitGame()
// - when you earn points, call GM.AddPoints(1234);
// - when you want to display score, get it from GM.Score (see GMShowScore.cs below)
// - when you want to display lives, get it from GM.Lives
//
// To persistently store high scores, see https://pastebin.com/VmngEK05
//
public static class GM
{
public static int Score;
public static int Lives;
// TODO: go nuts if you want to add more stuff like health and wave and coins, etc.!
public static void InitGame()
{
Score = 0;
Lives = 4;
}
public static void AddScore( int points)
{
Score += points;
}
}
using UnityEngine;
// @kurtdekker
// Put this ON the UnityEngine.UI.Text object you're using to display score
public class GMShowScore : MonoBehaviour
{
int lastScore;
void UpdateToText()
{
var text = GetComponent<UnityEngine.UI.Text>();
// Be SURE this GameObject has a UnityEngine.UI.Text component on it!
text.text = "Score:" + lastScore;
}
void Start()
{
UpdateToText();
}
void Update ()
{
if (GM.Score != lastScore)
{
lastScore = GM.Score;
UpdateToText();
}
}
}
@kurtdekker
Copy link
Author

kurtdekker commented Sep 11, 2021

Read comments at the top of each file for integration. In total you will need to:

  • create each of the files above, properly named (GM.cs and GMShowScore.cs) in your project
  • call GM.InitGame() at the start of your entire game (once per game)
  • call GM.AddScore() to add points to your score (or just set GM.Score directly yourself!)
  • optional: use GM.Lives to track lives
  • optional: add any other properties you want such as Coins, Health, etc.
  • put the GMShowScore.cs on a GameObject in your UI that has a UnityEngine.UI.Text Component on it

NOTE: you do NOT put GM.cs into your scene: it is a pure static data container.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment