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 GameManager 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 GameManager for each scene ? I don't see the purpose of using DontDestroyOnLoad on your GameManager 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 GameManager ?
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 …
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.
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?
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?
@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
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)
There are no singletons in Unity, Unity is an ECS system.
Could you provide more details ? Like the contents of your GameManager 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 GameManager for each scene ? I don't see the purpose of using DontDestroyOnLoad on your GameManager 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 GameManager ?
– cornYes, 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
– Fattieif it's not necessary you can make them non trigger objects so they will never enter inside each other .
– Ali-hatem