changing between Play and Pause in one click of a button

Hi Everyone
I want to change the Music Status from Play and Pause
i found a script online that makes the music run in different scenes
and the same person gave a way to pause the music…and it works
i’ve tried Playing and Pausing using different buttons…and it worked too :heart:
but now i want do it all in one button
i’ve tried different methods, but sadly i couldn’t get it to work
i hope you can help me :heart:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PauseMusic : MonoBehaviour
{
    public void PauseMusicOnClick()
    {
        if (Input.anyKeyDown)
        {
            OnGoingMusic.Instance.gameObject.GetComponent<AudioSource>().Pause();
        }
        else
        {
            OnGoingMusic.Instance.gameObject.GetComponent<AudioSource>().Play();
        }

    }
}

Try something like this:

public class PauseMusic : MonoBehaviour
 {
     private bool musicPaused = true;
     public void PauseMusicOnClick()
     {
        musicPaused = !musicPaused

        if(musicPaused){
            OnGoingMusic.Instance.gameObject.GetComponent<AudioSource>().Pause();
        } else{
            OnGoingMusic.Instance.gameObject.GetComponent<AudioSource>().Play();
        }
 
     }
 }