- Home /
Creating A Proper Game Manager
I am trying to learn how to use a game manager properly or some other solution. From what I understand, I store things in it that are not just variable to the one scene, or may want to be recapped latter.
For example I cant find any answer on how to rig Unity.UI to the game manager properly. I end up with more than one instance of a game manager due to scenes loading.
How do I work around this? This seems to not be a common topic of discussion. What is the proper way to do this, because I understand that their are many ways to get the job done. What way is the best?
Could you provide more details ? Like the contents of your Game$$anonymous$$anager script, the structure of your prefab, the calls on your buttons' onClick events... all things that are relevant.
Also, why do you need a different Game$$anonymous$$anager for each scene ? I don't see the purpose of using DontDestroyOnLoad on your Game$$anonymous$$anager only to have it destroyed by another object right after the loading of the second scene. Wouldn't it be more convenient to use a single Game$$anonymous$$anager ?
Yes, like Corn I am mystified by the comments about the buttons. Just mark the buttons DDOL
You must have a "pre-launch" scene (100% of projects have to do this), just put them in there as DDOL
This is ubiquitous in Unity. Indeed it often leads to discussions such as ... http://answers.unity3d.com/questions/441246/editor-script-to-make-play-always-jump-to-a-start.html
Answer by Fattie · Jan 10, 2016 at 03:20 PM
This entire issue is
INCREDIBLY OUT-OF-DATE
There's just nothing to it these days ...
CLICK HERE for an explanation -
http://stackoverflow.com/a/35891919/294884
it is dead simple. There's just nothing to it.
If you do want to use the Grid.cs script - note that the only benefit is you save typing ONE line of code - just go here for the latest version ...
http://answers.unity3d.com/answers/663681/view.html
Cheers.
How is this not upvoted a hundred times already? It should be required reading. I've only been learning Unity for two months, and a third of that time was wasted trying/failing to make singletons work. Now the scales fall from my eyes and my scenes transition like butter. Thank you, Fattie, for the Grid! So simple ... brilliant brilliant brilliant.
Hi @ChristopherWood - right, there can no more be "singletons" or even "classes" in Unity than you could have OO and Classes in say Photoshop or $$anonymous$$icrosoftWord.
It's just that there is a huge amount of incredibly out of date trash on the issue on the web, so there is a lot of confusion
It is just this simple ...
Thankyou!!! I've been learning Unity and c# for the last couple of years and I've really struggled with how to implement a proper game manager. I've finally got my combat/floating text manager working!! :D
Question: Why does every project need a preload scene??
I now have a preload scene before my main menu scene - but I'd like to know, what does this do that my main menu scene wasn't already doing? Surely it's just placing the __holder object?
Correct, the raison d'etre of a preload scene is simply to hold all of your "You need them everywhere" components (like sound effects, scoring etc). You're 1000% correct.
Unity famously do some staggering silly things. The fact that they forgot to inherently include a preload scene in Unity is just beyond human belief. It's very likely they will do it in Unity7 and the issue will never come up again.
It's the most absolutely silly thing in all of Unity.
In your post on this subject at Stackoverflow, you said, "Note that you can not use "splash screen" or "menu" as the preload scene." Out of curiosity, why? For a while, I was using a completely empty preload scene to deploy my "general behaviors", but after a time, it just seemed unnecessary, so I started using my Splash scene as a preload scene. If there is some significant downside to this, could you explain to me what that is?
The only real reason to not use the splash screen would be a case where the amount of data handled in the "general-needed behaviors" is so great that it has a immediate impact on the splash screen content. Having the game hanging even just 1 sec during the beginning of the splash screen isn't really a good start. By having a separated preload scene, you ensure that the game only "hang" during the App/Software opening, before the splash screen even starts.
Answer by Fritsl · Nov 02, 2017 at 10:08 AM
@Fattie: Thanks for elaborating all this, it's great!
There is a point though that people are trying to get through to you, and I'll just give it a go as well, and since this is spread across to StackOverflow, forgive me a double post, but I think this is rather important to know for newbees specially since this post is high in Google ranking:
Having a "Preload" is a very good tip etc. But you take a step further, and there's an issue there.
We do not want every instantiation of everything in our mobile games to do a "FindObjectOfType" for each and every every "global class"!
Instead you can just have it use an Instantiation of a static / a Singleton right away, without looking for it!
And it's as simple as this: Write this in what class you want to access from anywhere, where XXXXX is the name of the class, for example "Sound"
public static XXXXX Instance { get; private set; }
void Awake()
{
if (Instance == null) { Instance = this; } else { Debug.Log("Warning: multiple " + this + " in scene!"); }
}
Now instead of your example
Sound sound = Object.FindObjectOfType<Sound>();
Just simply use it, without looking, and no extra variables, simply like this, right off from anywhere:
Sound.Instance.someWickedFunction();
You could make the logging stronger using DebugError("Error: multiple instances detected", this); Debug will take the current GameObject as a second parameter
Answer by Notter · Jan 10, 2016 at 04:00 PM
You don't need a prefab of the GameManager. Just put the script on an object in your first scene.
make the script a singleton, and make sure to add "DontDestroy(gameObject); this will make it so the object will stay alive between scenes, which means you don't need to place it in every scene.
that being said.. if you want to test other scene without loading the first scene, then you'll need to place the gameManager in each scene (but remember to remove/disable it later)
What you're describing is just a singleton that has all your other singletons inside it.
And i don't understand why a singleton can't be a $$anonymous$$onoBehavior. in the Rogue-like tutorial they show an example of a Game$$anonymous$$anager and Audio$$anonymous$$anager which are Singletons.
No, it's not a singleton dude. Singletons don't really work in the c#-Unity environment.
It's actually just a static class that you use "trickily" in a way that looks and performs exactly like a real singleton.
It's kind of syntactic sugar you know? Easy!
Your answer
![](https://koobas.hobune.stream/wayback/20220612055643im_/https://answers.unity.com/themes/thub/images/avi.jpg)