0
\$\begingroup\$

This script is attached to another scene that is not the main menu scene:

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

public class LoadMainMenuOnclick : MonoBehaviour
{
    private Scene scene;

    // Use this for initialization
    void Start ()
    {
        scene = SceneManager.GetActiveScene();
    }

    // Update is called once per frame
    void Update ()
    {
        if (scene.name != "Menu")
        {
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                SceneManager.LoadSceneAsync(0);
            }
            //To check that if in gameplay mode
            //f in The Space Station scene or any other scene
            //that is not the main menu scene only then to be able
            //to click on escape key to load the main menu
            //and to check why the exit button and sound not working
            //when clicking pressing the escape key.

            // And here to also pause the game !!!!!
            // to check how to pause the game here when
            // clicking the escape key.
        }
    }
}

What I want is when I click the escape key to load the main menu scene that index 0 and then to pause the game. I don't want to unload or remove the current active scene when pressing escape I wan to keep both scene loaded when loading the main menu.

Not sure if to use LoadSceneAsync or just LoadScene.

When I start the game both scenes are loaded and the active scene is not the main menu but I see and use the main menu scene.

Then I click on a start game button and it's unloading the main menu scene:

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

public class LoadScenes : MonoBehaviour
{
    // Use this for initialization
    void Start ()
    {  
        SceneManager.LoadScene(1, LoadSceneMode.Additive);
        StartCoroutine(WaitForSceneLoad(SceneManager.GetSceneByName("The Space Station")));        
    }

    public IEnumerator WaitForSceneLoad(Scene scene)
    {
        while (!scene.isLoaded)
        {
            yield return null;
        }
        SceneManager.SetActiveScene(SceneManager.GetSceneByBuildIndex(1));
    }
}

When the game is running there are two loaded scenes: "Main Menu" and "The Space Station" The active scene is the "The Space Station" but the scene that load first is the main menu.

When I click the Start Game button it's unloading the Main Menu scene.

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

public class LoadSceneOnClick : MonoBehaviour
{
    public void ActivatePlayer()
    {
        GameControl.player.SetActive(true);
        SceneManager.UnloadSceneAsync(0);
    }
}

Now when I press escape I want to get back to the main menu scene to load it again and to pause the "The Space Station" scene.

I also want to save the game progress so far when pressing on escape. So it will pause the game but also will save the progress so far to a file on the hard disk.

\$\endgroup\$
1
\$\begingroup\$

Once Unity loads a new scene, the old one is unloaded automatically, all the GameObjects and scripts are destroyed (see it as a reset) and can't be accessed anymore (even if you have a reference).

You can use DontDestroyOnLoad on GameObjects, which makes Unity keep them and add them to the new scene. This means that you can share scripts between scenes, e.g.:

You're using a Slider to simulate a health bar. Switching to a new level (=scene) would destroy this health bar GameObject but if you use DontDestroyOnLoad in the script attached to it and reference it, the health bar will be in the next level too (with the same value it was before switching scenes).

Doing this for every GameObject in a scene isn't adviced, though.

What you can do:

  1. Have a single, big scene with two "parent" objects, one for the main menu, one for your space station. It will take longer to load the overall game but if every GameObject in the scene is a child of one of the "parent" objects, you can change "scenes" by setting the parent of the main menu inactive and the parent of the space station active, which will then also deactivate/activate all of its children.

  2. You can combine 1. with LoadSceneMode.Additive, which you're already using and which just adds the objects of the new scene to the old scene without destroying anything.

  3. (recommended) Have two separate scenes and always only load one of them but save the progress from the second scene when you go back to the main menu. To do this just create a static class, which doesn't derive from MonoBehaviour and also isn't attached to a GameObject. This script then won't get destroyed when you switch between scenes and you can save information like the player position. This information you can later just write to a file to be able to restore everything after restarting the game.

Then switching scenes is easy: Just add a script to an invisible GameObject in your "space station" scene:

public class InputManager : MonoBehaviour {
    void Update() {
        if(Input.GetKeyDown(KeyCode.Escape)) {
            //Save stuff
            SceneManager.LoadScene("MainMenu");
        }
    }
}

Difference between LoadScene and LoadSceneAsync:

LoadScene loads the new scene and tries to instantly display it. If it's taking a while, your game will freeze.

LoadSceneAsync doesn't display the new scene but loads it in the background and only displays it once it's actually done (careful: it's still running on the main thread). This way you can create a loading screen that keeps the user updated with a loading bar.

\$\endgroup\$
1
\$\begingroup\$

A simple solution for your problem would be to go a bit "outside of the box". Instead of creating the menu as a separate scene, make it a part of UI.

Then, during the gameplay, on press escape - pause the game and show the UI element.

In my opinion this is the easiest way to achieve what you are looking for.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.