5
\$\begingroup\$

I am building a first person shooter game using unity and I am facing the problem of resuming a scene from its current state. It would be easy to pause the scene by using the Time.timeScale = 0.0; within the scene itself. But I am loading a pause scene to ask whether the user wants to restart, resume or quit. But when I click on resume, it loads the scene I want, but it is totally fresh (i.e when the Ammo is supposed to be half full, it is full. Or the enemy has lost a part of his life, but then it starts with life completely full)

How do I resume the previous scene from where it left off using SceneManager? This could have been somewhat easy by using the Application.loadLevel() and making sure that application does not destroy the previously loaded state.

Here is the script I made for the pause scene.

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

public class pauseScript : MonoBehaviour {


    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }

    public void exitPressed()
    {
        Application.Quit();
    }

    public void restartPress() 
    {
        SceneManager.LoadScene("prototype1");
    }

    public void resumePress()
    {
        SceneManager.LoadScene("prototype1");
    }
}

Here is the code snippet for calling the pause menu scene. and this is in another script.

SceneManager.LoadScene ("PauseMenu");

Is there anyway to save the current scene before calling the PauseMenu scene as shown in the above statement? This may be a simple enough query to you but I am completely new to game development as a whole and I am using unity for the first time, so please go easy on me.

Thanks

\$\endgroup\$
5
\$\begingroup\$

You can use SceneManager with LoadSceneMode.Addtive. By that you will be able to load another scene on your current scene.

And when you want to resume just unload Pause Scene using UnloadScene

//===============================================================================//
// Gameplay.cs
public bool _paused = false;

// Pausing Game
void PauseGame()
{
    SceneManager.LoadScene ("YOUR_PAUSE_SCENE", LoadSceneMode.Additive);
}
//===============================================================================//


//===============================================================================//
// PauseMenu.cs

// Resuming Game
void ResumeGame()
{
    Gameplay gp = FindObjectOfType<Gameplay>();
    gp._paused = false;
    SceneManager.UnloadScene ("YOUR_PAUSE_SCENE");
}
//===============================================================================//

Tip: Remove Camera from your Pause Scene as LoadingScene in Additive mode will add all your game objects of pause scene in current scene.

\$\endgroup\$
  • \$\begingroup\$ Thanks for the advice mate, just one more thing, I put in a bool paused in my playerControl script to indicate whether the game has been paused or not. How do I set it to false upon the SceneManager.UnloadScene ("YOUR_PAUSE_SCENE"); in my pauseScript script? \$\endgroup\$ – Pavan Vasan Feb 16 '16 at 10:17
  • 1
    \$\begingroup\$ Several ways to do that.. Either you can get a public variable of bool paused in script where you are calling UnloadScene by SCRIPT_CONTAINING_PAUSED_VARIABLE obj = FindObjectOfType<SCRIPT_CONTAINING_PAUSED_VARIABLE>(); obj.paused = false; before UnloadScene, or you can use delegates which'd be little more tricky. Let me know if first one doesn't work, then I'll update my answer with Delegates implementation. \$\endgroup\$ – Hamza Hasan Feb 16 '16 at 10:32
  • \$\begingroup\$ check the edits by the way :) \$\endgroup\$ – Hamza Hasan Feb 16 '16 at 10:42

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.