- Home /
Where do I put gamewide constant variables
If I have 2 squad type. Regular and Elite.
A regular squad can have 9 units per squad, and an Elite squad can have 4. A squad spawner may refer to this data, different squad that is already spawned may refer to this data when getting reinforced etc etc.
I feel like the "max number of units" setting should be stored in a database or a static data class. so it only exist in 1 place.
public enum SquadType
{
Regular,
Elite,
}
[System.Serializable]
public class SquadSettings
{
public SquadType squadType;
public int MAX_NUM_OF_UNITS;
}
[SerializeField] List<SquadSettings> SquadSettings = new List<SquadSettings>();
Where do I put this SquadSettings list?
Maybe a Singleton GameManager script attached to an empty gameobject that persist through each scene?
Answer by sean244 · Jan 09, 2019 at 10:51 PM
You should definitely put it inside of a singleton. What your describing sounds exactly like a manager class - which would be a singleton. So you can either put it in your GameManager, or something more specific, like a UnitManager. But yes, a singleton is definitely the way to go.
Also as a general follow up question. A complete game would have lots of settings like these. Do I just make a bunch of different singletons, like a Unit$$anonymous$$anager for all unit related settings, a $$anonymous$$usic$$anonymous$$anager for all music related settings? and put all those Singleton in the same empty game object. Is this how everyone does it? It feels logical.
Of course. This is very, very common. $$anonymous$$ost games have several different managers: A Game$$anonymous$$anager will handle general things like pausing, restarting, and quitting the game; a Level$$anonymous$$anager will handle things having to do with the actual level, like opening a gate after all the enemies are killed; a UI$$anonymous$$anager will handle things having to do with the user interface, like displaying the pause menu or main menu; of course an Audio$$anonymous$$anager is standard practice; and a Unit$$anonymous$$anager is also very common for mmorpgs and rts games. It's very standard to have multiple singletons in your game. It's practically the norm.
[EDIT]
I just re-read your question and you asked if all of these mangers should be attached to the same gameObject? No. They definitely should not be attached to the same gameObject. Each $$anonymous$$anager exists as it's own gameObject. So in the hierarchy, you should see one gameObject called 'Game$$anonymous$$anager', another one called 'Audio$$anonymous$$anager'...so on. If you want to see an example of this, you can clone my platformer game that I made in Unity https://github.com/sean244/Braid-Clone It uses several different manager classes like the ones I described above. I'm also working on a rts game of my own if you want to see a preview of that https://www.youtube.com/watch?v=9ELUwr9k42g
Your answer
![](https://koobas.hobune.stream/wayback/20220612185444im_/https://answers.unity.com/themes/thub/images/avi.jpg)