- Home /
UI Button's Images Resets after Scene Reloads, Script Attached
Hey guys, I have pretty much finished my very first game and I'm stuck with this. The below script does exactly what I want. It mutes and unmutes the audio on the scene. What my problem is, when I press the button and mute, my UI Button's Image changes to the Muted Image, the audio is also muted, but when I die and I restart the scene, the UI Image for the button resets back to Unmute Button, even though the sound remains muted. This is my Restart Script
void Update() {
if (playerDead == true)
{
restartText.SetActive(true);
if (Input.GetMouseButtonDown(0))
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
And this is my Audio Script, both are different scripts, the death script is what I use to reload the scene and to trigger different different animations, the audio snippet come's under my UI buttons script, I use this to trigger the in game and main menu related scripts.
public void ToggleAudio(UnityEngine.UI.Image spriteMusicToggle) {
if (AudioListener.volume == 0)
{
AudioListener.volume = 1;
spriteMusicToggle.sprite = spriteMusicOn;
}
else if (AudioListener.volume == 1)
{
AudioListener.volume = 0;
spriteMusicToggle.sprite = spriteMusicOff;
}
}
I haven't studied PlayerPrefs yet, because this is a single level game, the story starts and ends on the first level itself and there are no checkpoints or anything and the only thing that I need to implement in the game and use is a single Mute Button that toggle's itself on and off. P.S I'm not using toggle, I'm using UI Button, just for clarification :)