Creating a Simple Game Manager in Unity
A Game Manager in Unity is a central script or system that oversees and controls the core mechanics and logic of a game. It typically manages game states (e.g., start, pause, end), player progress, scores, levels, and global settings. The Game Manager is crucial for ensuring a clean structure in your project and avoiding redundant code. In this tutorial, we’ll create a basic Game Manager to handle game state and score tracking.
Step 1: Setting Up the Scene
To begin, prepare a simple Unity scene:
- Create a new Unity 3D project.
- Add a basic level layout, such as a Plane for the ground and a few objects for gameplay elements.
- Create a UI Canvas to display information like the score.
Step 2: Creating the Game Manager Script
The Game Manager will be a singleton to ensure it is easily accessible and only one instance exists throughout the game. Let’s write the script:
- Right-click in the Project panel and select Create > C# Script. Name it GameManager.
- Open the script in your code editor and replace its contents with the following:
using UnityEngine;
public class GameManager : MonoBehaviour
{
public static GameManager Instance; // Singleton instance
public int score = 0; // Player's score
public bool isGameActive = true; // Is the game running?
void Awake()
{
// Ensure there is only one instance of GameManager
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject); // Persist across scenes
}
else
{
Destroy(gameObject); // Destroy duplicate instances
}
}
// Method to increase the score
public void AddScore(int points)
{
if (isGameActive)
{
score += points;
Debug.Log("Score: " + score);
}
}
// Method to end the game
public void EndGame()
{
isGameActive = false;
Debug.Log("Game Over!");
// Additional logic to handle game over, like displaying UI
}
}
Step 3: Adding the Game Manager to the Scene
Now, let’s add the Game Manager to the scene:
- Create an empty GameObject in the Hierarchy and name it GameManager.
- Drag and drop the GameManager script onto the GameObject.
- Ensure there are no duplicate GameManager GameObjects in the scene.
Step 4: Updating Gameplay Logic
Let’s make use of the Game Manager to handle player interactions. For example, we’ll modify gameplay objects to interact with the Game Manager:
- Create a new script called Target for objects the player can interact with.
- Use the following code for the Target script:
using UnityEngine;
public class Target : MonoBehaviour
{
public int pointValue = 10; // Points awarded for interacting with this target
void OnMouseDown()
{
if (GameManager.Instance.isGameActive)
{
// Add points to the score
GameManager.Instance.AddScore(pointValue);
// Destroy the target
Destroy(gameObject);
}
}
}
This script assumes the player interacts with objects by clicking on them. You can adapt it to your game’s mechanics, such as collisions or triggers.
Step 5: Displaying the Score
To display the score to the player:
- Create a Text UI element in the Canvas and name it ScoreText.
- Create a new script called ScoreUI and attach it to the Canvas.
- Use the following code to update the score display:
using UnityEngine;
using UnityEngine.UI;
public class ScoreUI : MonoBehaviour
{
public Text scoreText;
void Update()
{
if (GameManager.Instance != null)
{
scoreText.text = "Score: " + GameManager.Instance.score;
}
}
}
Drag the ScoreText UI element into the ScoreText field in the Inspector.
Step 6: Testing the Game Manager
To test your Game Manager:
- Run the scene and interact with objects that use the Target script.
- Observe the score updating in the UI as you interact with targets.
- Call the EndGame method (e.g., using a trigger or button) to test ending the game.
Optional Enhancements
You can expand the Game Manager’s functionality with these features:
- Level Management: Load new levels or reset the scene on game over.
- Game Timer: Add a countdown timer to challenge players.
- Saving Progress: Store and retrieve player progress using PlayerPrefs or a save system.
- Pause Menu: Implement a pause menu to stop the game and show options.
Conclusion
We've created a simple Game Manager to manage game state and score tracking in Unity. The Game Manager centralizes core logic, making your project more organized and scalable. Experiment with additional features to tailor the Game Manager to your game’s needs.