[C#, 2D, iOS] How to switch between active scenes properly?

Hi

Right now, I’m developing iOS game with Unity 2D Platformer.

I would like to know how to switch between active scenes properly. Just like UITabBar in iOS

What I did :

  1. Create “VeryFirstScene” which has main UI with four buttons to change scenes and set main UI “DontDestroyOnLoad”

  2. Add listener to buttons to set each scene active.

  3. Load four scenes with LoadSceneMode.Additive when game starts.

  4. Click buttons to switch between added scenes.

My code :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class MainUIController : MonoBehaviour {

    public Button characterButton;
    public Button dessertButton;
    public Button ovenButton;
    public Button fridgeButton;

    private Scene sceneToActive;

    void Awake() {
        DontDestroyOnLoad (transform.gameObject);
    }
  
    void Start () {
        characterButton.onClick.AddListener (CharacterScene);
        dessertButton.onClick.AddListener (DessertScene);
        ovenButton.onClick.AddListener (OvenScene);
        fridgeButton.onClick.AddListener (FridgeScene);

        SceneManager.LoadScene ("CharacterScene", LoadSceneMode.Additive);
        SceneManager.LoadScene ("DessertScene", LoadSceneMode.Additive);
        SceneManager.LoadScene ("OvenScene", LoadSceneMode.Additive);
        SceneManager.LoadScene ("FridgeScene", LoadSceneMode.Additive);
    }

    void CharacterScene (){
        sceneToActive = SceneManager.GetSceneByName ("CharacterScene");
        SceneManager.SetActiveScene (sceneToActive);
    }

    void DessertScene () {
        sceneToActive = SceneManager.GetSceneByName ("DessertScene");
        SceneManager.SetActiveScene (sceneToActive);
    }

    void OvenScene() {
        sceneToActive = SceneManager.GetSceneByName ("OvenScene");
        SceneManager.SetActiveScene (sceneToActive);
    }

    void FridgeScene() {
        sceneToActive = SceneManager.GetSceneByName ("FridgeScene");
        SceneManager.SetActiveScene (sceneToActive);
    }
}

Result :

When game starts, it has scene hierarchy like this (bold one is active):

  • VeryFirstScene
  • CharacterScene
  • DessertScene
  • OvenScene
  • FridgeScene
  • DontDestroyOnLoad

It shows FridgeScene and Main UI which set to be “DontDestroyOnLoad”.

After I clicked CharacterScene button to set CharacterScene active,

scene hierarchy changed like this :

  • VeryFirstScene
  • CharacterScene
  • DessertScene
  • OvenScene
  • FridgeScene
  • DontDestroyOnLoad

and no changes in the game view. It shows FridgeScene and main UI.

How can I make active scene always on the top of other scenes so that hierarchy can change like this when I click the button :

  • VeryFirstScene
  • DessertScene
  • OvenScene
  • FridgeScene
  • CharacterScene
  • DontDestroyOnLoad

I’ve been googling for 3 days but I can’t find answer…

You should either load the scenes only when needed or hide their content until needed. According to the documentation, “active scene” simply refers to the scene where new GameObjects are added.

1 Like

So… Is there no way to do that?

I want four scenes to be active while user plays the game.

For example…

  • User does something in OvenScene and until It’s done, he/she loads other scenes and look around waiting for the job is done.

  • User’s character in the CharacterScene moves around the world in the CharacterScene and the character keeps on moving around the world in CharacterScene while user visits other scenes.

The active scene is also used for the lighting settings. Fog, skybox, ambient lighting, that kind of stuff.

When you load a scene additively, it’s added to the other scenes. It will be part of the game world like any other scene that’s loaded at the time. It doesn’t matter whether it’s active or not. If you don’t want a scene to be loaded and part of the game, don’t load it.

2 Likes

OK… I understand it.

I think I should try different way

I came up with the idea that manage all data in one empty object and make it dontDestroyOnLoad.

I’ve made a tool to help with this if you still want to load additive scenes in that manner:

1 Like

I made one empty object in the first scene of my game and add codes that I want to be dontDestroyOnLoad.

It works great. Thank you all!!

@Selzier I’ll use your tool If I need more complex scene manipulation. thank you!