Singleton Script
Let us make a GameManager Script which will be singleton and will not destroy between scene loading or changing.
First of all create empty Gameobject in hierarchy inside unity game engine, rename it to "GameManager".
Now click on Add Component inside the inspector panel and create a new script namely "GameManagerScript".
Open GameManagerScript in Visual Studio/ Monodevlop,
Write the following code :
using UnityEngine;
public class GameManagerScript : MonoBehaviour
{
public static GameManagerScript instance;
void Awake()
{
if(instance != null)
{
Destroy(gameobject);
}
else
{
instance = this;
DontDestroyOnLoad(gameobject);
}
}
}
Comments
Post a Comment