Game Programming Pattern: Singleton

Objective: create a game manager script and use the singleton pattern.

Ok now I have to create a game manager for the great fleece. This script is going to have some functions and properties to control the state of the game.

In this script we have to create a private static variable of the same type of the script (in this case GameManager) and usually we call it _instance and also create a public property called Instance to allow other objects access to this instance.

By doing this we have only one instance of GameManager that can be accessed by any part of the game. This makes the communication between objects and the control of the state of the game.

To initialize the instance we have to use the Awake() function, check if the instance is null and assign instance = this. If for some reason you create another instance of a game manager the null check is going to avoid duplication and we are gonna keep only one instance of a game manager.

The last thing I added to this script is a property called HasKey, this is a bool that indicates if the player collected the key card. I am gonna use it in the other script to try the game manager.

So I created a new script called GrabKeyCardActivation and assigned it to the collider at the sleeping guard zone. Then I declared a variable to store the reference to the sleeping guard cutscene and by using the OnTriggerEnter function I just checked for the player tag and activated the object.

Then I created another script called WinStateActivation and assigned it to the collider at the end of the level.

In the script I created a reference to the win cutscene and by using again the OnTriggerEnter function I checked for the player tag and activate the win cutscene.

And that’s it!!! Basic use of the singleton Pattern.

--

--

--

A Software Engineer passionate about game dev and interactive products with Unity. I consider video games to be the artistic expression of programming.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Generalised Algebraic Data Types

Step 1: START ASKING! FOSSMEC GSoC Session Report

Flutter for Mobile Apps as a Trendsetter in 2022

Flutter for mobile apps

Advent Of Code — JSAbacusFramework.io — Puzzle 12

Why your company should start using dbt… with an example

How to prepare for your Software Engineering Bootcamp…

Busting the Barriers of your Browser with FriendUP

Introducing the Triple Laser

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rusben Guzman

Rusben Guzman

A Software Engineer passionate about game dev and interactive products with Unity. I consider video games to be the artistic expression of programming.

More from Medium

New Input System and UI

Creating a Manager Classes in Unity

Getting Started with AWS for Unity

Designing Enemies using Abstract Classes