Search Unity

Music On And Off Functionality Not working.

Discussion in 'Getting Started' started by Galactic_Void, Aug 19, 2023.

  1. Galactic_Void

    Galactic_Void

    Joined:
    Jun 5, 2023
    Posts:
    2
    Hello, basically am trying to create a functionality in which I can toggle music on and off in my settings scene, and then, when I return to my main menu scene. The music will be On/Off depending on my settings. Pretty simple, right? But I've been trying for over 2 hours and really can't seem to get this to work.
    I apologize if my scripts are messy but I've been trying many different things so that's the reason :/
    Please notify me of any errors in my code:

    The first script is my MusicToggleScript which toggles the button's on and off.

    it look like this:

    using UnityEngine;
    using UnityEngine.UI;

    public class MusicToggleScript : MonoBehaviour
    {
    public Button musicOnButton; // Assign your Music On Button in the Inspector
    public Button musicOffButton; // Assign your Music Off Button in the Inspector

    private bool isMusicOn = true; // Default state is music on

    private void Start()
    {
    // Retrieve the music state from PlayerPrefs
    int isMusicOnValue = PlayerPrefs.GetInt("IsMusicOn", 1); // Default to 1 (on) if not found
    isMusicOn = isMusicOnValue == 1;

    // Initialize the buttons based on the retrieved music state
    SetButtonVisibility();

    // Add click event listeners to both buttons
    musicOnButton.onClick.AddListener(ToggleMusic);
    musicOffButton.onClick.AddListener(ToggleMusic);
    }

    // This method is called when either button is clicked
    private void ToggleMusic()
    {
    // Toggle the music state
    isMusicOn = !isMusicOn;

    // Update the button visibility based on the music state
    SetButtonVisibility();

    // Save the updated state in PlayerPrefs
    PlayerPrefs.SetInt("IsMusicOn", isMusicOn ? 1 : 0);
    PlayerPrefs.Save();
    }

    private void SetButtonVisibility()
    {
    // Set the button visibility based on the music state
    musicOnButton.gameObject.SetActive(isMusicOn);
    musicOffButton.gameObject.SetActive(!isMusicOn);
    }

    // This method allows other scripts to check the music state
    public bool IsMusicOn()
    {
    return isMusicOn;
    }
    }


    And then is my MainMenuMusicPlayer script, which is basically supposed to play the music.

    MainMenuMusicPlayer (It's in the Main Menu scene not the settings scene duh)


    using System.Collections;
    using UnityEngine;

    public class MainMenuMusicPlayer : MonoBehaviour
    {
    public AudioClip soundtrack; // Assign your soundtrack in the Inspector
    private AudioSource audioSource;
    private bool musicIsOn = true; // Default state is music on

    private void Start()
    {
    int isMusicOn = PlayerPrefs.GetInt("IsMusicOn", 1); // Default to 1 (on) if not found
    musicIsOn = isMusicOn == 1;

    audioSource = GetComponent<AudioSource>();
    audioSource.clip = soundtrack; // Set the clip initially

    if (musicIsOn)
    {
    StartCoroutine(PlayMusicLoop());
    }
    }

    private IEnumerator PlayMusicLoop()
    {
    while (musicIsOn)
    {
    yield return new WaitForSeconds(1f);

    audioSource.Play(); // Play the audio

    yield return new WaitForSeconds(audioSource.clip.length); // Wait for the track to complete

    audioSource.Stop(); // Stop the audio before the next iteration

    yield return new WaitForSeconds(10f); // Wait 10 seconds before looping
    }
    }
    }

    I don't know where I went wrong?
    I've been trying many different things for hours on end, PLEASE HELP
     
  2. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,789
    well the entire clip must play out before it will consider stopping.. Maybe you should look there first. If its music, normally these are minutes or more even in length
     
  3. Galactic_Void

    Galactic_Void

    Joined:
    Jun 5, 2023
    Posts:
    2
    It does play out before stopping?
    Thats why I have the line of code saying:
    yield return new WaitForSeconds(audioSource.clip.length);
     
  4. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,789
    yes but normally one expects it to stop immediately not in say 20 minutes when the item is done right?