Search Unity

Pause Menu Not hiding onClick

Discussion in '2D' started by amaturemanga, Jun 22, 2019.

  1. amaturemanga

    amaturemanga

    Joined:
    May 30, 2019
    Posts:
    25
    Hi there im working on a pause menu and i have a couple of issues with it firstly when i call the pause menu it appears but the game doesnt actually pause the dialogue still types out. And secondly when i click on the button that has the onClick event to hide the pause menu it doesnt hide it despite the button having the onClick event. Im not too worried about the actually pausing for now, ill work on that next my main concern right now is why its not hiding the pause menu any ideas?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6.  
    7. public class DialogueScript : MonoBehaviour
    8. {
    9.    
    10.     public  static int TextboxCounter = 0;
    11.     public int index = 0;
    12.     public int referenceToTextboxCounter;
    13.     DialogueSystem dialogue;
    14.  
    15.     public GameObject pauseMenu;
    16.  
    17.     public static bool isPaused = false;
    18.  
    19.     public string[] s;
    20.  
    21.  
    22.     private void Awake()
    23.     {
    24.         referenceToTextboxCounter = TextboxCounter;
    25.     }
    26.  
    27.     void Start()
    28.     {
    29.         dialogue = DialogueSystem.instance;
    30.         dialogue.Say(s[index]);
    31.         index++;
    32.         TextboxCounter ++;
    33.         referenceToTextboxCounter++;
    34.        
    35.     }
    36.    
    37.     // Update is called once per frame
    38.     void Update()
    39.     {
    40.         if(isPaused == false)
    41.         {
    42.             if (Input.GetMouseButtonDown(0))
    43.             {
    44.                 if (!dialogue.isSpeaking || dialogue.isWaitingForUserInput)
    45.                 {
    46.                     if (index >= s.Length)
    47.                     {
    48.                         SceneChange();
    49.  
    50.                     }
    51.                     Say(s[index]);
    52.                     index++;
    53.                     TextboxCounter++;
    54.                     referenceToTextboxCounter++;
    55.  
    56.  
    57.  
    58.  
    59.                 }
    60.             }
    61.             else if (Input.GetKeyDown(KeyCode.Return))
    62.             {
    63.                 if (!dialogue.isSpeaking || dialogue.isWaitingForUserInput)
    64.                 {
    65.                     if (index >= s.Length)
    66.                     {
    67.                         SceneChange();
    68.  
    69.                     }
    70.                     Say(s[index]);
    71.                     index++;
    72.                     TextboxCounter++;
    73.                     referenceToTextboxCounter++;
    74.  
    75.  
    76.  
    77.  
    78.                 }
    79.             }
    80.             else if (Input.GetKeyDown(KeyCode.Space))
    81.             {
    82.                 if (!dialogue.isSpeaking || dialogue.isWaitingForUserInput)
    83.                 {
    84.                     if (index >= s.Length)
    85.                     {
    86.                         SceneChange();
    87.  
    88.                     }
    89.                     Say(s[index]);
    90.                     index++;
    91.                     TextboxCounter++;
    92.                     referenceToTextboxCounter++;
    93.  
    94.  
    95.  
    96.  
    97.                 }
    98.             }
    99.  
    100.         }
    101.  
    102.  
    103.        
    104.  
    105.         if (Input.GetMouseButtonDown(1))
    106.         {
    107.             if (pauseMenu != null)
    108.             {
    109.                 isPaused = true;
    110.                 pauseMenu.SetActive(true);
    111.             }
    112.         }
    113.  
    114.  
    115.  
    116.  
    117.  
    118.     }
    119.  
    120.     void Say(string s)
    121.     {
    122.         string[] parts = s.Split(':');
    123.         string speech = parts[0];
    124.         string speaker = (parts.Length >= 2) ? parts[1] : "";
    125.        
    126.         dialogue.Say(speech,  speaker);
    127.  
    128.        
    129.     }
    130.  
    131.     public void SceneChange()
    132.     {
    133.        
    134.        
    135.  
    136.         if (TextboxCounter == 10)
    137.         {
    138.             SceneManager.LoadScene(7);
    139.         }
    140.         else if (TextboxCounter == 21)
    141.         {
    142.             SceneManager.LoadScene(8);
    143.         }
    144.         else if (TextboxCounter == 32)
    145.         {
    146.             SceneManager.LoadScene(9);
    147.         }
    148.  
    149.         else if(TextboxCounter == 42)
    150.         {
    151.             SceneManager.LoadScene(10);
    152.         }
    153.     }
    154.  
    155.    
    156.  
    157.     public void returnToGame()
    158.     {
    159.         if(pauseMenu.activeSelf && isPaused == true)
    160.         {
    161.             if(Input.GetMouseButtonDown(0))
    162.             {
    163.                 pauseMenu.SetActive(false);
    164.                 isPaused = false;
    165.             }
    166.            
    167.         }
    168.     }
    169.  
    170. }
     
  2. Cressidali

    Cressidali

    Joined:
    Feb 19, 2018
    Posts:
    13
    Try this:
    Code (CSharp):
    1.     public void ReturnToGame()
    2.     {
    3.         if (pauseMenu.activeSelf && isPaused == true)
    4.         {
    5.             pauseMenu.SetActive(false);
    6.             isPaused = false;
    7.         }
    8.     }
    The problem was that this function doesn't get called on the same frame as the mouse click/button click so the second if returns false because if is GetMouseButtonDown. I hope that helps.
     
  3. amaturemanga

    amaturemanga

    Joined:
    May 30, 2019
    Posts:
    25
    ok so i tried that and it still not working
     
  4. Cressidali

    Cressidali

    Joined:
    Feb 19, 2018
    Posts:
    13
    That's weird, it worked fine with me.

    Make sure that the function is selected in the OnClick on the button. If it's selected then add this line on the start of the function:

    Code (CSharp):
    1. Debug.Log(isPaused);
    to see if isPaused isn't turning false somehow.
     
  5. amaturemanga

    amaturemanga

    Joined:
    May 30, 2019
    Posts:
    25
    yea its not showing up in the console as pressed so the issue is its not changing to false for some reason
     
  6. Cressidali

    Cressidali

    Joined:
    Feb 19, 2018
    Posts:
    13
    if you've put the debug before the if statement and it's not showing up then the function hasn't been called. Is the Gameobject that has the script and the function in the script selected in the Onclick on the button?
     
  7. amaturemanga

    amaturemanga

    Joined:
    May 30, 2019
    Posts:
    25
    yea they're both selected in the onClick
     
  8. amaturemanga

    amaturemanga

    Joined:
    May 30, 2019
    Posts:
    25
    in the script it says there's no references in ReturnToGame
     
  9. Cressidali

    Cressidali

    Joined:
    Feb 19, 2018
    Posts:
    13
    My visual studio auto corrected returnToGame to ReturnToGame, if you copied and pasted the code I've sent you then it may have made a conflict, if so retype the function's name to "returnToGame".
     
  10. amaturemanga

    amaturemanga

    Joined:
    May 30, 2019
    Posts:
    25
    no i changed it to that also in the button so its really weird why its not detecting it i even created a bool to check if its detecting the press and its not but when i click on the bool so its true the menu hides like it should it just doesnt detect the button is being pressed in game
     
  11. amaturemanga

    amaturemanga

    Joined:
    May 30, 2019
    Posts:
    25
    oh never mind just tried it again and now it works this was really strange but thanks
     
  12. Cressidali

    Cressidali

    Joined:
    Feb 19, 2018
    Posts:
    13
    I'm still confused, but you're welcome. :)