Different behaviour (Game view versus standalone exe)

The situation is, I have two scenes - a main menu scene, and a game scene.

A) In main menu scene (blank screen with a few buttons) the user selects a file to load, then I will load the game scene.

B) In the game scene (prefabs in the background), once the game is over, a GUI button will appear which, when pressed, will load the main menu scene again.

The thing is, everything works as described while I test on the unity game view. But when I run it as a standalone exe and complete the game, clicking the return button in B does bring back the menu from A, but all the prefabs and buttons still remain in the background superimposed. In unity game view, it properly returns me to the blank screen in scene A. Whats up with the difference in behaviour there?

moved and reformated from comment

// SCENE A CONTROLLER
void OnGUI()
{
    if (GUI.Button("Load"))
    {
        DontDestroyOnLoad(gameObject);
        Application.LoadLevel("GameScene");
    }
}

In Scene B, once the game is over, a button will appear which should bring player back to scene A when clicked.

// SCENE B CONTROLLER
void Awake()
{
    GameObject obj = GameObject.Find("SceneAController");
    if (obj != null)
    {
        // populates scene with prefabs based on selected file 
        LoadScenario(obj.GetComponent().filename);
    }
    Destroy(obj);
}

void OnGUI()
{
    if (GameOver)
    {
        if (GUI.Button("Main Menu"))
        {
            Application.LoadLevel("MainScene");
        }
    }
}

You shouldn’t use Awake for accessing other gameobjects. The order in which Awake is executed on the different objects doesn’t have to be the same in the editor and in a build. You can specify the execution order per script type, but it’s better to use Start instead of Awake. In Start all gameobjects are initialized and are actually there.

Also use DontDestroyOnLoad carefully. Usually it’s better to use a dedicated object to chare information across scenes.

Btw, your GetComponent is missing the type, right? Was it the generic version? Then it propably got interpreted as HTML tag since you didn’t mark it as code.

After some random experiments, I finally found the reason why the Scene B objects were still being displayed after loading back to Scene A.

I had disabled my Camera script in Scene A, thinking I didn’t really need it since Scene A only needed a GUI with black background. Turning it back on has solved the problem.

I still have no idea why this only happens in standalone and not in unity editor, but ah well at least its working. In the future, I’ll have to make it a point to test my game in exe rather than relying on the editor.

Thanks for the help and tips, syclamoth and Bunny :slight_smile: