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.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