Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Why when clicking on escape key back to main menu the main menu is not working ?

Discussion in 'Scripting' started by Chocolade, Jul 28, 2018.

  1. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    916
    1 down vote favorite


    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:



    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class LoadSceneOnClick : MonoBehaviour
    8. {
    9.    public void LoadByIndex(int sceneIndex)
    10.    {
    11.        SceneManager.LoadScene(sceneIndex);
    12.    }
    13. }
    14.  
    On the main scene I added a empty gameobject and attached to it another script:

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class BackToMainMenu : MonoBehaviour
    7. {
    8.    public int sceneIndex;
    9.  
    10.    // Update is called once per frame
    11.    void Update ()
    12.    {
    13.        if (Input.GetKeyDown(KeyCode.Escape))
    14.        {
    15.            UnityEngine.SceneManagement.SceneManager.LoadScene(sceneIndex);
    16.        }
    17.    }
    18. }
    19.  
    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.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I don't think your problem has to do with how you are loading the scene. What does "but this time I can't click on any of the buttons in the main menu" specifically mean? Do you not see the buttons? Can you click on them but they just don't do anything?

    Do you have any console errors?
    Do you have the ESC key set to do something in your main menu scene?
     
    Chocolade likes this.
  3. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    916
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    However you are locking the cursor, I'd just undo it either right before opening the menu scene, or put unlocking the cursor into a Start method on some script in the menu scene. There's several settings related to locking the cursor, and you haven't posted any code related to how you are doing it, so I can't say exactly what code to use to undo it.
     
    Chocolade likes this.
  5. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    916
    Ok working now. I just added new script to the main menu on empty gameobject that just bring back the mouse cursour.

    Code (csharp):
    1.  
    2. Cursor.visible = true;
    3. Cursor.lockState = CursorLockMode.None;
    4.  
    Now it's working great. Thanks.
     
    Joe-Censored likes this.
  6. farrellart

    farrellart

    Joined:
    Dec 14, 2019
    Posts:
    16
    I had this same issue! Thank you!!!!
     
  7. drgavin

    drgavin

    Joined:
    Dec 28, 2021
    Posts:
    2
    I had the same issue as well. Unlock your cursor in the main menu scene!
    Code (CSharp):
    1. void Start() {
    2.   Cursor.visible = true;
    3.   Cursor.lockState = CursorLockMode.None;
    4. }
    This fixed everything