1
\$\begingroup\$

I have two scenes. Main Menu and the main scene. The game start when only the main menu is loading.

When I click on the New Game button it's loading the main scene and removing the main menu scene.

If I click now on the Escape key it will remove the main scene and load back the main menu scene but this time I can't click on any of the buttons in the main menu.

I'm not sure if this is the right way to switch between the scenes but it's not working good.

On the Newgame button I created and attached a new script. And then in the OnClick event of the button I'm calling the method LoadByIndex:

Main Menu

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

public class LoadSceneOnClick : MonoBehaviour
{
    public void LoadByIndex(int sceneIndex)
    {
        SceneManager.LoadScene(sceneIndex);
    }
}

On the main scene I added a empty gameobject and attached to it another script:

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

public class BackToMainMenu : MonoBehaviour
{
    public int sceneIndex;

    // Update is called once per frame
    void Update ()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            UnityEngine.SceneManagement.SceneManager.LoadScene(sceneIndex);
        }
    }
}

When the game start in the main menu when clicking on New Game button it's loading index 1 the main scene and removing the main menu.

When clicking on escape key it's loading index 0 and removing the main scene. But then the main menu is not working.

\$\endgroup\$
  • \$\begingroup\$ The problem was that in the game in the main scene I locked the mouse cursor and also make the mouse cursor not to be visible. So what I did is attached new script to the main menu to a empty gameobject with two lines in the Start: Cursor.visible = true; Cursor.lockState = CursorLockMode.None; And now everything is working great. I can click on the escape key at any time and place in the game back to the main menu and click on new game and it's starting the game over. \$\endgroup\$ – jdoedoe doedoe Jul 28 '18 at 0:01
  • \$\begingroup\$ You can write an answer to that effect if you like, or delete the question if you don't want to leave it open. \$\endgroup\$ – DMGregory Jul 28 '18 at 1:59

Your Answer

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

Browse other questions tagged or ask your own question.