Search Unity

Keep button animation and Button Audio while pause and after pause?

Discussion in 'Scripting' started by shawnrevels, Jun 29, 2015.

  1. shawnrevels

    shawnrevels

    Joined:
    Aug 13, 2014
    Posts:
    86
    Ok, so i have a game that im making that uses interactive buttons. On the screen i have score, time, and an exit button. I also have numerous buttons alligned on the screen that the player clicks to define which color is different. each button has an animator and an audio. Push the button and it plays an animation and a click sound. If you click the Exit button a message window pops up asking if you would like to exit the game. At this point the Timer pauses. If you hit cancel it goes back to the game level. The timer then continues but the button animations and the sound doesnt work anymore...? Below is my scripts i use. As well as the approaches i took to fix the problem. Any help would be awesome!! Thanks.

    The StopTime Script
    Code (JavaScript):
    1.  
    2. function OnGui() {
    3.    
    4.         doPauseToggle();
    5.     }
    6.    
    7.  
    8. function doPauseToggle() {
    9.     // here we check to see if we are running at a time scale above 0
    10.     if(Time.timeScale<0){
    11.         // time scale is above zero, so we need to pause the game here
    12.         pauseGame();
    13.     }
    14. }
    15. function pauseGame () {
    16.     // set scale at which time passes to 0, freezing time(!)
    17.     Time.timeScale=0;
    18.    AudioListener.pause = false;
    19.    
    20. }
    StartTime Script

    Code (JavaScript):
    1.    
    2.         doPauseToggle();
    3.     }
    4.    
    5.  
    6. function doPauseToggle() {
    7.    
    8.     if(Time.timeScale<0){
    9.        
    10.         unPauseGame();
    11.     }
    12. }
    13.  
    14. function unPauseGame () {
    15.  
    16.     Time.timeScale=1;
    17.     AudioListener.pause = false;
    18.     Animator.speed = false;
    19.    
    20. }
    CountDownTimer Script

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5.  
    6.  
    7.  
    8. public class CountDownTimer : MonoBehaviour {
    9.  
    10.  
    11.     public float startingTime;
    12.  
    13.     private Text theText;
    14.  
    15.  
    16.  
    17.  
    18.  
    19.     void Start () {
    20.  
    21.  
    22.         theText = GetComponent<Text>();
    23.     }
    24.  
    25.     void Update (){
    26.  
    27.         //response = 0;
    28.         float startTime = Time.time;
    29.  
    30.         startingTime -= Time.deltaTime;
    31.  
    32.         if (startingTime <= 0) {
    33.             Destroy (gameObject, 16);
    34.  
    35.  
    36.  
    37.                 Application.LoadLevel("Game_Over");
    38.                
    39.  
    40.         }
    41.  
    42.         theText.text = "" + Mathf.Round (startingTime);
    43.     }
    44. }
    45.  
    46.  
    MenuManager Script that handles switching to another menu as well as other things

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3.  
    4. public class MenuManager : MonoBehaviour
    5. {
    6.  
    7.  
    8.     [SerializeField]
    9.     private string m_animationPropertyName;
    10.  
    11.     [SerializeField]
    12.     private GameObject m_initialScreen;
    13.  
    14.     [SerializeField]
    15.     private List<GameObject> m_navigationHistory;
    16.  
    17.     public void GoBack()
    18.     {
    19.         if (m_navigationHistory.Count > 1)
    20.         {
    21.             int index = m_navigationHistory.Count - 1;
    22.             Animate(m_navigationHistory[index - 1], true);
    23.  
    24.             GameObject target = m_navigationHistory[index];
    25.             m_navigationHistory.RemoveAt(index);
    26.             Animate(target, false);
    27.         }
    28.     }
    29.  
    30.     public void GoToMenu(GameObject target)
    31.     {
    32.  
    33.         //Time.timeScale = 0;
    34.  
    35.  
    36.         if (target == null)
    37.         {
    38.             return;
    39.         }
    40.  
    41.         if (m_navigationHistory.Count > 0)
    42.         {
    43.             Animate(m_navigationHistory[m_navigationHistory.Count - 1], false);
    44.         }
    45.  
    46.         m_navigationHistory.Add(target);
    47.         Animate(target, true);
    48.         AudioListener.pause = false;
    49.  
    50.     }
    51.  
    52.     private void Animate(GameObject target, bool direction)
    53.     {
    54.         if (target == null)
    55.         {
    56.             return;
    57.         }
    58.  
    59.         target.SetActive(true);
    60.  
    61.         Canvas canvasComponent = target.GetComponent<Canvas>();
    62.         if (canvasComponent != null)
    63.         {
    64.             canvasComponent.overrideSorting = true;
    65.             canvasComponent.sortingOrder = m_navigationHistory.Count;
    66.         }
    67.  
    68.         Animator animatorComponent = target.GetComponent<Animator>();
    69.         if (animatorComponent != null)
    70.         {
    71.             animatorComponent.SetBool(m_animationPropertyName, direction);
    72.         }
    73.     }
    74.  
    75.     private void Awake()
    76.     {
    77.         m_navigationHistory = new List<GameObject>{m_initialScreen};
    78.  
    79.     }
    80.  
    81.     public void QuitApplication()
    82.     {
    83.         Application.Quit();
    84.     }
    85.  
    86.  
    87. }
    Now the timer works as it should. When i click the exit button the timer stops using the stop time script that is attached to the Exit Button. Then when you Exit the message panel the time starts again. Yet the buttons no longer work. I cant click the blue boxes in the game. See the image for instance.
    If you look at the Start and Stop Time Scripts youll see i added
    1. AudioListener.pause = false;
    Which doesnt work. and i tried
    1. Animator.speed = false;
    Which also doesnt work.
     

    Attached Files:

  2. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    shawnrevels likes this.
  3. shawnrevels

    shawnrevels

    Joined:
    Aug 13, 2014
    Posts:
    86
    I set all button animations to Unscaled Time.
     

    Attached Files:

  4. shawnrevels

    shawnrevels

    Joined:
    Aug 13, 2014
    Posts:
    86
    Im going to upload a video to show you exactly the problem.
     
  5. shawnrevels

    shawnrevels

    Joined:
    Aug 13, 2014
    Posts:
    86
    Problem Fixed. Both Canvases were in a main canvas. the message canvas was fine, but the level1 canvas needed to be deleted. so i just drug out the level1 items from the canvas and it worked.