I am pretty new to C#…and trying to learn C# and Unity…so i am writing a small game. I learned about the Singleton pattern and all. And made previous games with singleton classes like a “LevelLoader” and a “Music Player”. Then i read this article:
https://connect.unity.com/p/singleton-scriptableobject
And scriptable objects are also new to me. But i think i get it. In on of my earlier games i had a “GameData” class that implements the singleton pattern. But i remember it was a pain to test in other scenes. So this is a pretty good class to turn into a Singleton Scriptable Object (it just holds values that travels from scene to scene).
However, this class:
public class LevelLoader : MonoBehaviour
{
public static LevelLoader Instance { get; private set; }
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(this);
}
else
{
gameObject.SetActive(false);
Destroy(gameObject);
}
}
public void LoadSummaryScene()
{
SceneManager.LoadScene("SummaryScene");
}
public int GetActiveSceneIndex()
{
return SceneManager.GetActiveScene().buildIndex;
}
public string GetActiveSceneName()
{
return SceneManager.GetActiveScene().name;
}
public void LoadSceneByIndex(int _index)
{
SceneManager.LoadScene(_index);
}
public void LoadSceneByName(string _name)
{
SceneManager.LoadScene(_name);
}
}
This class is used in several scenes…but it holds no values…so this is better to use it as a static class right? Because there is no values to edit and it needs to be accessed through several scenes.
If it would hold alot of properties it would be better as a Singleton SO?
Am i right?