Hey,
I am trying to save my toggle button all the time with the help of playerprefs. If you press the toggle button the music should be muted, but it will be unmuted again when you restart the scene…
This is my script attached to the toggle button:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class toggleMusic : MonoBehaviour
{
public AudioSource Music;
public void Start()
{
PlayerPrefs.GetInt("Muted");
Music.mute = false;
}
public void ToggleSound()
{
if (PlayerPrefs.GetInt("Muted", 0) == 0)
{
PlayerPrefs.SetInt("Muted", 1);
}
else
{
PlayerPrefs.SetInt("Muted", 0);
}
SetSoundState();
}
private void SetSoundState()
{
if (PlayerPrefs.GetInt("Muted", 0) == 0)
{
AudioListener.volume = 1;
Music.mute = false;
}
else
{
AudioListener.volume = 0;
Music.mute = true;
}
}
}