In the RogueLike tutorial (my fave right now), they:
- write a GameManager script
- attach it to a GameManager object in the editor
- make that object a prefab
- delete the original object from the hierarchy
- write a Loader script (to load the GameManager)
- add the Loader script to the Main Camera
- insert GameManager prefab into Loader script slot
So I feel like I *almost* understand why to do this, but I can’t quite put my finger on it, so that means I could use a little help from google.
Helpful quote from redframe-game.com about Unity’s object-oriented programming and how gamemanagers are typically used. In fact, just go read their site instead of reading this post. (For me I like how RogueLike did it better, because the scripting looks simpler, but maybe there’s sthing to be said for how redframe does it? Nonetheless, nice to read about how things are typically done and why they’re useful. )
“There is no concept of low-level application code outside of the core Unity engine – there are only objects and their individual behaviors. The most common approach to implementing global managers in Unity is to create a prefab that has all manager scripts attached to it. You may have a music manager, an input manager, and dozens of other manager-like scripts stapled onto a single monolithic “GameManager” object. This prefab object would be included in the scene hierarchy in one of two ways:
- Include the prefab in all scene files.
- Include the prefab in the first scene, and call its DontDestroyOnLoad method during Awake, forcing it to survive future level loads.”
Gosh people think about singeltons in Unity a lot.
Ooh! And here’s a best practices site. Just what I’ve been looking for! Doesn’t really talk much about gamemanagers or singletons, but it looks useful to read through in general. (#todo!)
Possibly useful intro tutorial with screen grabs and written descriptions instead of video. (#todo – glance through and see if there’s anything I’m missing from that.)
This looks pretty similar to the RogueLike game in terms of board setup and levels. Would be interesting in going through it (#todo).
Looks like part of my problem was refering to GameManager.AddScore(), not GameManager.instance.AddScore();
Also, Roguelike tutorial to the rescue. This time I needed to look through the player script to undertsand : https://unity3d.com/learn/tutorials/projects/2d-roguelike/player-script?playlist=17150
OMG the reason my score isn’t working anymore is that now I have two scores – one that lives in my GameManager prefab, and one that lives in my hierarchy. I guess I delete it from the hierarchy and have it live in the prefab…?
I put ScoreText as a child of the GameManager prefab, and then retrieve its GUIText using GetComponentInChildren() inside the GameManager script (below). Not sure if this is the right way to do this, but it works for me now, and extends the score across levels, which is useful for me.
scoreText = gameObject.GetComponentInChildren<GUIText>();
#todo – add in more thoughts on this, organize. Thinking about programing as manipulating nouns vs. verbs – I’m used to verbs, so nouning the GameManager is a little hard for me.