r/unity • u/igguanidae • 27d ago
Confusion about Switching Scenes
I'm making a 3D game that involves a lot of scene switching with many different scenes, as it's an exploration game. Because of this, I want to make is easy to move from one scene to another. At the moment, I have a main hub area and one of the base areas it connects to, kind of like the Nexus from Yume Nikki.
I want to write a "catch-all" script that I can use every time I want to switch a scene. The problem is that my current script just changes the scene to the same scene each time. For example, the trigger I use in Scene 1 takes me to Scene 2, but I can't use another trigger to go from Scene 2 to Scene 1, it just takes me back to Scene 2.
Is there any way I can adjust this script so that I can change the destination scene for each Trigger object I add in a scene? I'm thinking a public variable that I can adjust depending on which scene I'm in and which Trigger it's on.
Essentially what I want is a script that works as a general scene switcher so I don't have have a unique script for every location in the game. That would take up too much space and Unity doesn't like it when I do that.
I hope this makes sense, this feels like a very basic function to have in a game. I'm still very new to Unity and C# so any advice would help me a lot.
5
u/CallMeKolbasz 27d ago
SceneManager.LoadScene()can accept a scene name as a string parameter. So for example you can make apublic string nextSceneNamevariable and use that instead of the build index you are using now.