You've successfully subscribed to Hacking with Unity - Learn how to create your own games
Great! Next, complete checkout for full access to Hacking with Unity - Learn how to create your own games
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info is updated.
Billing info update failed.

Sharing global data among different scenes

Ben Liu
Ben Liu

Global data can help us to share data or variables among different classes, even in different scenes.  Used scenarios such as game options including music volume, selected character's skin and so on.

It is always a good approach to have global variables in one place:

public static class GameSettings {

	// Max number of lifes that our character can have
	public static readonly int LIVES = 3;

	// Music level
	public static float musicVolume = 50.0;
}

Then in other scene class file, we can use the above variable:

if (currentLifes == GameSettings.LIVES) {
	// do some logic here
}

Ben Liu

Please feel free to contact me if you have any question. <br> https://t.me/NeedoneApp