LoadScene Problems

I am experiencing some weird behavior. I am making a VR game in Unity - I have a menu scene that is a full small level and when you press a UI element it loads another scene.

If I load the scenes individually then everything works perfectly - however, when I transition from the menu into the scene - the player controller position is changed. I wrote a script to fix this - however now when I go from the menu, to the scene, and back to the main menu, the player position there is changed. I even tried using a static class to determine if it is the first time the scene is loaded and to position things differently to try and overcome this behavior - but I think maybe I am just misunderstanding something. Is the player controller from the menu scene being taken into the second scene and maybe that is what is causing this weird position shift? When I come back to the menu level, one of the images also doesn’t fully load that does load when you first start the game.

I don’t understand why the process of loading the level does not simply load it fresh - and I am wondering if some scene state is being carried over even though I haven’t specifically programmed that?

In addition - some of what I looked at when searching for answers brought up LoadSceneAsync which I would love to use as this doesn’t block when clicking the menu item - but that is giving me weird behaviour too. From the menu scene if I LoadSceneAsync(“level”) that’s fine, but then if I try and LoadSceneAsync back to (“Menu”) it crashes. Using adb logcat, I can see that the error it gives is a Null Reference, DeadGameObject error. So does LoadSceneAsync destroy the scene it has just come from? I am confused why I am not seeing other people mention this as being a thing so I am sure I am just going wrong somewhere.

Tl;dr
Loading A or B or C works fine. Using LoadScene going from A to B messes with the player position, but if I use a script to fix that then going A to B and back to A causes problems.
Using LoadSceneAsync going from A to B works fine. But going A to B back to A causes a crash saying A no longer exists.

Hi Chris,
I’ve definitely done this sort scene transition before in Unity (admittedly a while ago) so I can say for certain it is possible. Is there anything being tracked between the scenes using singletons that could disrupt the scenes? Are the scenes added to the build scenes?

Without seeing code, there’s probably little more that I can offer you.

Hi! Thanks a lot for the suggestions! The scenes are all in the build scenes, and the only singleton I added was to try and solve the problem. So initially - I had no singletons and got the behaviour that loading scene B had everything in the right place, but transitioning from A to B means things are out of place. I added a singleton to basically tell scene B it had been transitioned to.

Do you know if there is any behaviour with either OVRPlayerController or XROrigin that maybe uses singletons? I’m wondering if it isn’t something I’ve coded but maybe the existing controllers have that functionality anyway!

Also - I’m happy to share code but I’m not 100% sure which parts might be useful! I have quite a complicated setup. I can share my level loading script if that helps?

I had more weird behaviour when trying to fix it last night. If I use LoadSceneAsync to go from scene A to B and then try and LoadSceneAsync back it crashes and gives a DeadGameObject error - but if I LoadScene from A to B and then LoadSceneAsync from B back to A - it does then allow me to LoadScene to B again. Although doing this the other way round gave me a DeadGameObject error. That’s quite complicated to describe - but it is part of why I’m struggling so much is that I can’t even figure out a consistent logic as to what is happening! Ie - in the first example it would seem that LoadSceneAsync is destroying the previous scene, but in the second example that obviously isn’t the case - but all I did was swap the order?

Apologies for the massive rant! All the examples I can find just show a simple - use LoadScene or LoadSceneAsync to go from A to B and noone seems to have issues transitioning back, or with objects messing up but there doesn’t seem to be much more information about how it I should be doing it!

This is my SceneLoader script:

using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneLoader : MonoBehaviour
{
    public bool isMenu;
    public OVRInput.Button quitToMenuButton;
    private bool isDownPrevious = false;
    private bool isDown = false;

    public void LoadScene(string sceneName)
    {
        // Use a coroutine to load the Scene in the background
        SceneManager.LoadScene(sceneName);
        //StartCoroutine(LoadYourAsyncScene(sceneName));
    }

    void Update()
    {
        if (isMenu)
        {

        }
        else
        {
            //Set the previous
            isDownPrevious = isDown;
            //Get the new states
            isDown = OVRInput.GetDown(quitToMenuButton);

            //Check if state of button press has changed and if it's down - go back to the menu
            if (isDown != isDownPrevious && isDown)
            {
                //SceneManager.LoadScene(0);
                StartCoroutine(LoadYourMenuSceneAsync());
            }
        }

    }

    IEnumerator LoadYourMenuSceneAsync()
    {
        // The Application loads the Scene in the background as the current Scene runs.
        // This is particularly good for creating loading screens.
        // You could also load the Scene by using sceneBuildIndex. In this case Scene2 has
        // a sceneBuildIndex of 1 as shown in Build Settings.

        AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(0);

        // Wait until the asynchronous scene fully loads
        while (!asyncLoad.isDone)
        {
            yield return null;
        }
    }
}

So the first function is called by a UI action from the scene to load into my different scenes - and the second function takes a button press from one of those scenes to load back to the menu. I included both LoadScene and LoadSceneAsync and the reason I have commented out different ones is because this is the only way that didn’t crash and at least let me load to the scene, to the menu, and back again. Although some of the objects in the scene get messed up.

There’s nothing in there that jumps out. I don’t think that’s the problem. Have you tried setting up a really simple project to test out simple code like scene navigation. It’ll be easier to debug this way.

Privacy & Terms