Share needed variables by all gameObjects with a manager class


estimated time : 7 mn
ingredients needed : Unity , 1 GameObject containing the gameController script, 1 or more GameObjects that need to access values.

You want to have some variables that can be accessible between some gameObjects quickly?

Let’s say we want the speed bonus contained in one object collected by a player to affect the player speed.
How should we do that?
With the help of a global manager object and the use of singleton. Let’s play.


00:04 One Manager to rule them all

The main idea here is to have a script that will manage and access all the “critic” variables that other objects need to see at some point.
It could be the main score, the speed of a given player, anything that seems vital to be seen by other objects.

So create a gameManager script, and attach it to a main object (On my part, I prefer to include it on the mainCamera object, as it’s a required object that you cannot miss) with this code :

public class GameManager : Monobeahviour {
 
// a public static class is visible everywhere in the system and can be accessed by other classes
public static GameManager gameManager;
 
//the Instance() will be called by every other scripts that'll need to access to the variables.
public static GameManager Instance(){
 
//this will assure that only one instance of gameManager is created,
//if we didn't do this check, we'll have multiple instances of gameManager and the vars will be passed from one to another
//and it'll be hell.
if(!gameManager)
gameManager = FindObjectOfType(typeof (GameManager)) as GameManager;
 
return gameManager;
}
 
}

 

01:42 Accessing the manager by other classes

Now in the other objects that’ll need to access our global variables we’ll instantiate the gameManager

//declaration
private GameManager gameManager;
// in Start() function
gameManager = GameManager.Instance();
And now we can retrieve now each gameManager variables.
Let’s continue just for testing how it works:

02:48 Creating UI text to see our variables

We create an UI Text canvas, then we declare it in our player script as a public Text ;(don’t forget to use UnityEngine.UI to be able to access the Text reference)
Play with the text tools a little to be more visible.

03:52 Displaying our var

In our player script, just display our baseSpeed var  in the UI Text, by convert it as a String with :
displaySpeed.Text = baseSpeed.ToString();
We see now that our speed is displayed in our UI Text.

04:23 Accessing our GameManager vars

Now we create a playerSpeed int into our gameManger script.
Then in our player script we assign it to our player baseSpeed like this:

gameManager.playerSpeed = baseSpeed;
We test it  by displaying in our UI Text the value of the gameManager variable:

displaySpeed.text = gameManager.playerSpeed;

And It display “1”, so everything ok, it’s visible;

05:30 Creating a bonus Object

Now we create a bonus object with a bonus script on it, and we access the gameManager class from there too.
We tell it to update the gameManager.playerSpeed with it’s own bonus value :

gameManager.playerSpeed = bonusSpeed;

and once we test the script we see that everything’s working fine with multiple access to the gameManager vars, the player update displays now the bonusSpeed value.

 

 

Leave a Reply