GameManager Template in Unity

· 1 min read
GameManager Template in Unity
Game manager template in Unity

Many game developer are using GameManager to manage the game status, do initialization and more in their games. So it is recommend for anyone who wants to have the same in their games. Here is one template of GameManager.cs.

First of all, we create an empty game object in the scene, and attach the following c# script to it:

using UnityEngine;

public class GameManager : MonoBehaviour
{
    private int score = 0;

    void Start()
    {
        DontDestroyOnLoad(gameObject);
    }

    public void AddScore(int amount)
    {
        score += amount;
    }

    public int GetScore()
    {
        return score;
    }
}

First by introducing a custom class GameManager inherited from MonoBehaviour base class. Suppose we want to keep an record on the score that player has won. The AddScore method is used to increase the score, and the GetScore method is used to retrieve the score. To use this game manager, call the AddScore and GetScore methods from other scripts as needed.