- Home /
Statics Best Practice
I'm setting up the management system for a game project I'm working on, which at its core has a GameManager class which carries across all scenes via singleton. Within each scene is a LevelManager class which holds properties specific to that level (player start position, etc.). A reference to the current LevelManager is added to the GameManager when a scene launches, and is set to null when the scene changes. I'm currently assigning the LevelManagers to a Static property, but I'm wondering if there is any point to doing so if I'm going to reference the GameManager instance anyway. Do people have an opinion on whether it is better to keep it as a static or just use a public property?
public class GameController : MonoBehaviour
{
public string helloWorld = "Hello World";
private static GameController _instance;
public static GameController Instance
{
get
{
return _instance;
}
}
private static LevelController _currentLevelController;
public static LevelController currentLevelController
{
get
{
return _currentLevelController;
}
}
void Awake()
{
if (_instance == null)
{
_instance = this;
Debug.LogWarning("GameController assigned on awake.");
}
}
public void AssignLevelController(LevelController levelControllerInstance)
{
Debug.Log("LevelController unassigned: " + currentLevelController);
_currentLevelController = levelControllerInstance;
Debug.Log("LevelController assigned: " + currentLevelController);
}
}
public class LevelController : MonoBehaviour
{
private GameController gameController;
public string property = "Property Recieved!";
void Awake()
{
gameController = GameController.Instance;
gameController.AssignLevelController(this);
}
}
public class GetLevelControllerProperties : MonoBehaviour
{
private LevelController levelController;
void Start()
{
levelController = GameController.currentLevelController;
StartCoroutine(GetProperty());
}
IEnumerator GetProperty()
{
yield return new WaitForSeconds(5f);
Debug.Log(GameController.Instance.helloWorld);
Debug.Log(levelController.property); //Has to access through the GameController
}
}
Answer by logicandchaos · Jan 12, 2021 at 10:09 PM
Don;t use singletons! They are the DEVIL! :P Unity's scriptable objects make singletons unnecessary, the pattern itself it not too bad, but how they are abused. You have a GameManager singleton that has don't destroy on load and I bet tons of unrelated data, just used to save across scenes. ScriptableObjects don't live in scenes so you could use one as a GameManager, but that is still a big monolithic class, which is really the root to all the problems. You need to use scriptable object architecture, make scriptable object variables that you can plug in anywhere. https://www.youtube.com/watch?v=raQ3iHhE_Kk&t=1051s
I usually don't $$anonymous$$d recommending these "bad" practices like Singletons when I the one in question isn't yet very familiar with code, because it would be too much for they to handle anyway.
I don't think so learning it the SO way 1st may make things easier, I wish I knew about SOs when I 1st started with unity, I would be so much further long right now. I did forget that sometimes a single can be utilized, but they should only do a single purpose, code architecture is really important and I wish I learned more of it from the start, it's harder when you have to unlearn all the bad practices.
You definitely have a point but I don't try to enforce "best" practices too much on beginners because it's so easy to give up when you're just learning the basics. Even having a little coding background when I started I struggled big time in the first months.
I always recommend watching the talk from this point on. It's just too good to be skipped ;)
Thanks, I watched the video, then watched it again to take notes! Our lectures only went as far as making us aware that scriptable objects existed but it's great to have actual use cases demonstrated. Kind of wish I'd know about it a few months ago, would have saved me a lot of time.
Ya I learned about them so late and they are so useful!
Your answer
![](https://koobas.hobune.stream/wayback/20220613023112im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Clamping acts strange with public static values? 0 Answers
When is a static game object static? 1 Answer
Animating object that has both static and non-static children 0 Answers
Calling Update function in another function (JS) 1 Answer
Why is my kill method not running the score system method call? 1 Answer