I m pretty noob to Unity and am trying to make a 2D metroidvania game. I have been looking for tutorials for a few days but couldnt find any that works for me.
I am trying to load next scene when the player reaches certain part of the current scene. My initial idea looked something like this:
Add Scenes 0,1, 2 & 3 to hierarchy. Scene 0 is all the player and camera and stuff that doesnt change between scenes. Scene 1 is the entrance to the castle. Scene 2 & 3 are subsequent scenes. Scene 2 & 3 is unloaded in the hierarchy.
Setup a Collider around half way of Scene 1 with below script attached to load Scene:
Is this a correct way to set it up? I couldnt get Scene 2 to load so needless to say couldnt test the rest of it. Also, Is having the player as singleton the only way to do this? I have not tried it but have seen alot of negative relating to singletons. Thanks!
Welcome! I am slightly terrified of your username but as long as you wash your hands before you post, you’re welcome here.
It sounds like you’ve been doing your homework too! We love people like you!
Loading scenes additively is absolutely the way to go here. But I will warn that it can be fiddly to get it right. When you load a scene it is not present until the next frame (at the earliest), so you can’t do anything with it until later.
Based on this it might be best to have a single global scene manager scene, something that never unloads itself and just listens for messages from the trigger volume to know when to load or unload stuff. That way all your configuration for putting the bits together is in one simple scene.
Not sure I’d reach for a singleton for the player… what if he dies? It’s best when things die to Destroy() and recreate them, in my book anyway. Unity can support several different singleton patterns too, so not all are the same:
true C# singleton: as you expect, static instance, not a Monobehavior
Unity singleton: these come in two flavors:
ones that have an infinite (or significantly long) lifetime (eg, score keeper)
ones that go away when you load a new scene (eg, wave manager)
Accessing stuff via singleton static instances throughout your code may cause some compSci majors to start twitching but in practice it solves a lot of annoying problems and lets you keep things neat and tidy. And if you need to refactor, well, that’s why we call it SOFT-ware, isn’t it?
Thank you so much!! It took me days looking for tutorials and it only took me 30mins to have it semi working using send message.
lol I just couldnt come up with an account name.
I would add here that you can do that either with a GameObject or with a ScriptableObject. Too many ways for the same thing ^^’ In my inexperience, I used to do that with a GameObject prefab and DontDestroyOnLoad, but I changed it to the ScriptableObject worlkflow since a while, which is easier and more consistent (in my opinion).
Thank you both, this question helped me (along with other forum posts) to take an overview about the additive scene workflow.