How to load a scene from another, with an elevator connecting the two on Unity3d ?

Hello everyone, I’m having trouble with the loading system in Unity3D, especially with how to handle it when you want to go from one scene to another via a gameobject. What I mean by that is :

  • I have a GameObject, an Elevator;
  • The Elevator can go up or down, depending on what direction the elevator comes from. It can only go to another floor, which is always the same. So two elevators in two distinct scenes are corelated by ID;
  • The Elevator is in a State Machine, to better handle the multiple states it can assume;
  • The Elevator, when taken, triggers an animation (the doors closes, the elevator goes up or down), and when the animation ends, the script triggers a ElevatorLoadScene(int ElevatorID, string SceneName) method;

I almost never worked with the SceneManager in Unity, so I don’t know why my code is not good, but here it is :

/// <summary>
    /// Loads a scene when you use an elevator.
    /// </summary>
    /// <param name="SceneName">The scene's name.</param>
    /// <param name="ElevatorID">The elevator's ID.</param>
    public void ElevatorLoadScene(string SceneName, int ElevatorID)
    {
        Scene sceneType = SceneManager.GetSceneByName(SceneName);
        SceneManager.LoadScene(SceneName);

        // Check every elevator in the list.
        ElevatorList.Clear();

        if (sceneType.IsValid()) TMPobjects = sceneType.GetRootGameObjects();

        // For each GameObject with layer Elevator, add it to the list.
        foreach (GameObject gameObject in TMPobjects)
        {
            if (gameObject.layer == 12)
            {
                ElevatorList.Add(gameObject);
            }
        }

        // For each Elevator in ElevatorList, check if the ID is the right one.
        foreach (GameObject ElevatorGM in ElevatorList)
        {
            ElevatorObject ElevatorGMC = ElevatorGM.GetComponent<ElevatorObject>();
            if (ElevatorGMC.ElevatorID == ElevatorID)
            {
                // Assigns the elevator's position.
                if (ElevatorGMC.IsGoingUp) ElevatorGMC.ElevatorRenderer.transform.position = ElevatorGMC.ElevatorUpPosition;
                else ElevatorGMC.ElevatorRenderer.transform.position = ElevatorGMC.ElevatorDownPosition;

                // Assigns the player's position to the elevator's position.
                Vector3 elevatorPos = ElevatorGMC.ElevatorRenderer.transform.position;
                Quaternion elevatorRot = ElevatorGMC.ElevatorRenderer.transform.rotation;
                Player.PlayerInstance.transform.position = elevatorPos;
                Player.PlayerInstance.transform.rotation = elevatorRot;

                // Launches the elevator's next state.
                ElevatorGMC.ElevatorRenderer.SetActive(true);
                ElevatorGMC.SwitchState(ElevatorObject.comingWithThePlayerState);
                break;
            }
        }
    }

The code runs fine until it reaches the if (sceneType.IsValid()) TMPobjects = sceneType.GetRootGameObjects(); line, where it seems the scene is not valid. I simply want the player to be on the next scene ElevatorRenderer’s position, when I launch this method. I tried to find solutions on the internet, but now I just can’t seem to understand what I’m doing wrong. If someone is kind enough to take some time and explain what’s the problem, or simply redirect me, I would gladly appreciate. Thank you !

Scene sceneType = SceneManager.GetSceneByName(SceneName);

This is only going to give you a valid scene if the scene represented by SceneName is already loaded.

As per the docs https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.GetSceneByName.html:

So we know this won’t work for a scene that is not loaded yet. Indeed the Scene struct is only meant to represent already loaded scenes.

This:

sceneType.GetRootGameObjects();

Is of course even more egregious when the scene is not loaded yet.

Long story short - you need to load the scene before you can start interacting with it.

2 Likes

Okay I get it.
I did not specify in this post since I did not thought about talking about it at all, but I tried with a coroutine, using a yield return new WaitForSeconds(1f); and/or a yield return null; as specified in some other forum post. But it did not work anyway. So do you have a quick solution or a ressource that could give a working example ?

I don’t understand what this has to do with your issue. As I mentioned above you need to actually load the scene before you interact with it. Are you actually loading the scene somewhere? Because the code you shared doesn’t load the scene at any point.

The best way to be able to reference scenes which have not yet been loaded is by their index number in the Build Settings dialog box. The reason for this is that ONLY scenes which are mentioned in this dialog box are included in your build by default. By default, if it’s not in the list, it can’t be loaded. So have your TOP-OF-ELEVATOR scene understand what index represents your BOTTOM-OF-ELEVATOR scene, use that index to load the desired scene, and vice versa.

Oh god damnit I edited the script I pasted on here because there was a line that was useless, and did not check if everything was here.
In line 9 there is supposed to be a SceneManager.LoadScene(SceneName); line.
I’m sorry, I should have read my post twice.

I edited the script so you can see. It explains why I did not understand your first comment… Anyway, the thing is, the scene is supposed to be loaded right ?