Search Unity

Question Audio won't pause after current level restarts

Discussion in 'Audio & Video' started by Demon_xModRx, Jul 7, 2020.

  1. Demon_xModRx

    Demon_xModRx

    Joined:
    May 15, 2020
    Posts:
    3
    In this game, the background music continues to play after the level restarts, and if the song ends before the current level is completed, you have to redo the previous level. that is the basic mechanic, however for some reason when the level is loaded for the 1st time, the audio can pause and resume perfectly fine, but if the current level restarts, than the audio no longer pauses after pressing pause. does anyone have any suggestions as to how I can fix this?
    Code (CSharp):
    1.     [SerializeField]
    2.     string LastLevel;
    3.     public static AudioSource IGM;
    4.     private bool isPaused = false;
    5.     public static bool isPlaying = false;
    6.     public AudioClip audioClip;
    7.     private float _duration;
    8.     public UnityEvent onFinishSound;
    9.     public static bool DestroyMe = false;
    10.     private void Awake()
    11.     {
    12.  
    13.         IGM = GetComponent<AudioSource>();
    14.  
    15.     }
    16.     private void Start()
    17.     {
    18.  
    19.         if (!isPlaying)
    20.         {
    21.             isPlaying = true;
    22.             DontDestroyOnLoad(transform.gameObject);
    23.             Initialize();
    24.             StartCoroutine(WaitForSound());
    25.         }
    26.         else
    27.         {
    28.             Destroy(gameObject);
    29.         }
    30.     }
    31.     public void FixedUpdate()
    32.     {
    33.         if(DestroyMe)
    34.         {
    35.             Destroy(gameObject);
    36.             DestroyMe = false;
    37.         }
    38.         if (Input.GetKey(KeyCode.Escape))
    39.         {
    40.             if (isPaused)
    41.             {
    42.                 Resume();
    43.             }
    44.             else
    45.             {
    46.                 Paused();
    47.             }
    48.         }
    49.     }
    50.  
    51.     public void Initialize()
    52.     {
    53.         IGM.clip = audioClip;
    54.         IGM.Play();
    55.         _duration = audioClip.length;
    56.     }
    57.  
    58.     IEnumerator WaitForSound()
    59.     {
    60.         yield return new WaitForSeconds(_duration);
    61.         isPlaying = false;
    62.         SceneManager.LoadScene(LastLevel);
    63.         onFinishSound.Invoke();
    64.     }
    65.  
    66.     void Paused()
    67.     {
    68.         IGM.Pause();
    69.         isPaused = true;
    70.     }
    71.     void Resume()
    72.     {
    73.         IGM.Play();
    74.         isPaused = false;
    75.     }
     
  2. SkrenZz

    SkrenZz

    Joined:
    Apr 11, 2020
    Posts:
    26
    I am not sure if it is the case, but one suspicion I have which might cause this bug is these following lines:
    Code (CSharp):
    1. if(DestroyMe)
    2.         {
    3.             Destroy(gameObject);
    4.             DestroyMe = false;
    5.         }
    Note that I am not completely certain about it, but I think that since you are destroying the gameObject, the line that sets DestroyMe back to false possibly never runs?
    Maybe try setting DestroyMe to false before destroying the gameObject and checking whether your issue is solved...