Search Unity

Resolved [Solved] OnClick() Button Event is not working.

Discussion in 'Scripting' started by Ciyinei, Sep 4, 2020.

  1. Ciyinei

    Ciyinei

    Joined:
    Jul 20, 2020
    Posts:
    7
    Hi everyone, I've been trying to add a pause UI in my game, but when I click the button to unpause the game it doesn't work, only when I press the key escape it pauses and unpauses the game as intended.

    The script for unpausing the game is attached to an object that I use as a container to put all the UI stuff as a child, I also referenced this object in the On Click() button's section.

    I'm not sure if it is a bug of Unity, but it looks like it is, and here is why:

    This is the script for unpausing the game, kind of messy BTW
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6.  
    7. public class SceneHandler : MonoBehaviour
    8. {
    9.     [SerializeField] float reloadDelay = 0.5f;
    10.     [SerializeField] float nextLevelDelay = 1.2f;
    11.  
    12.     [SerializeField] Image transitionImage;
    13.     [SerializeField] float transitionTime = 1f;
    14.  
    15.     [SerializeField] Canvas pauseCanvas;
    16.     Color transitionImageColor;
    17.  
    18.     bool isFromStartScreen = true;
    19.     bool isPaused = false;
    20.     int currentScene;
    21.     int nextScene;
    22.     void Start()
    23.     {
    24.         transitionImageColor = transitionImage.color;
    25.         currentScene = SceneManager.GetActiveScene().buildIndex;
    26.         nextScene = currentScene + 1;
    27.         pauseCanvas.enabled = false;
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update()
    32.     {
    33.         if (isFromStartScreen)
    34.         {
    35.             StartTransition();
    36.         }
    37.  
    38.         if (Input.GetButtonDown("Cancel") && !isPaused)
    39.         {
    40.             PauseGame();
    41.         }
    42.  
    43.         else if (Input.GetButtonDown("Cancel") && isPaused)
    44.         {
    45.             ResumeGame();
    46.         }
    47.     }
    48.  
    49.     public void ResetLevel()
    50.     {
    51.         StartCoroutine(Reset());
    52.     }
    53.  
    54.     public void LoadNextLevel()
    55.     {
    56.         StartCoroutine(NextLevel());
    57.     }
    58.  
    59.     IEnumerator Reset()
    60.     {
    61.         yield return new WaitForSeconds(reloadDelay);
    62.         SceneManager.LoadScene(currentScene);
    63.     }
    64.  
    65.     IEnumerator NextLevel()
    66.     {
    67.         yield return new WaitForSeconds(nextLevelDelay);
    68.         SceneManager.LoadScene(nextScene);
    69.     }
    70.  
    71.     private void StartTransition()
    72.     {
    73.         if (transitionImageColor.a == 0)
    74.         {
    75.             isFromStartScreen = false;
    76.             return;
    77.         }
    78.         else
    79.         {
    80.             transitionImageColor.a -= transitionTime * Time.deltaTime;
    81.             transitionImage.color = transitionImageColor;
    82.         }
    83.     }
    84.  
    85.     public void ResumeGame()
    86.     {
    87.         Debug.Log("Resuming Game");
    88.         isPaused = false;
    89.         Debug.Log("isPaused bool set to false");
    90.         Time.timeScale = 1.0f;
    91.         Debug.Log("timeScale set to 1");
    92.         pauseCanvas.enabled = false;
    93.         Debug.Log("pauseCanvas disabled");
    94.     }
    95.  
    96.     private void PauseGame()
    97.     {
    98.         isPaused = true;
    99.         Time.timeScale = 0.00000001f;
    100.         pauseCanvas.enabled = true;
    101.  
    102.     }
    103.  
    104.     public bool IsPaused()
    105.     {
    106.         return isPaused;
    107.     }
    108. }
    I used Debug on ResumeGame() to check if it was a problem of my conditions, but it works fine, the code prints everything when I press the key escape to resume the game and also when I click on the UI button, but it doesn't unpause the game when I click the button as I mentioned.

    I read some previous posts about this and I didn't found a solution that worked, I also checked if the script was not attached to other objects or setting timeScale to a very small value other than zero, and none of them worked.
     
  2. Zer0Cool

    Zer0Cool

    Joined:
    Oct 24, 2014
    Posts:
    203
    The only idea i have is to check that disabling the canvas is disabling the GO with your script attached too and therefore the method is not working as assumed. So try to put your script on an empty GO in your scene (without your UI) and try if the script then works.
     
    Ciyinei likes this.
  3. gaglabs

    gaglabs

    Joined:
    Oct 17, 2019
    Posts:
    185
    Code (CSharp):
    1.  
    2. void FixedUpdate()
    3.        {
    4.            
    5.            if (paused == true)
    6.            {
    7.            paused = false;
    8.            Time.timeScale = 0;
    9.            pauseCanvas.SetActive (true);
    10.            Debug.Log ("'Pause' Called");}
    11.  
    12.        else
    13.            {
    14.            pause = true;
    15.            Time.timeScale = 1;
    16.            pauseCanvas.SetActive (false);
    17.            Debug.Log ("'Unpause' Called");}
    18.        }
    19.            
    20.    }
    21.  
    22.       public void PauseGame()
    23.        {
    24.           paused = true;
    25.          
    26.         }
    27.  
    28.        
    29.        public void UnPauseGame()
    30.        {
    31.           paused = false;
    32.          
    33.  
    34.         }
    35.  
    36.  
    37.  
     
    Last edited: Sep 5, 2020
  4. Ciyinei

    Ciyinei

    Joined:
    Jul 20, 2020
    Posts:
    7
    I tried the code but I have the same issue, as far as I know this should also work
     
  5. Ciyinei

    Ciyinei

    Joined:
    Jul 20, 2020
    Posts:
    7
    You were right, I added an empty object to the scene and referenced it from the button with the OnClick() function and it worked, thank you for your help!!!
     
  6. PierreCarlection

    PierreCarlection

    Joined:
    Aug 22, 2021
    Posts:
    1
    Hi all,

    I had a similar issue and the cause was the EventSystem object: I deleted it previously after some advice saying we don't need it. If you have buttons it's not true !
     
  7. fax58

    fax58

    Joined:
    Dec 27, 2021
    Posts:
    42
    Hi,
    same issue here. I created an empty GO to contain all my Canvas but, from a different scene, the Canvas was only showing, not working on click. I had the EventSystem but was outside the GO container, everything works after I put it inside. Hope it will help someone.