Mute Button with changing textures issue

Hello everybody,

I found the code below on the forums and it works fine when muting music and image sprites change accordingly. The issue is that when I change scenes, though the code remembers mute or unmute state, the sprite reverts back to the source image. Is there something i could change in the code to fix that so it remembers last used sprite state?

Many thanks

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class AudioToggle : MonoBehaviour {

    public Sprite spriteMusicOn;
    public Sprite spriteMusicOff;

    public void ToggleAudio (Image spriteMusicToggle) {
        if(AudioListener.volume == 0.0f) {
            AudioListener.volume = 1.0f;

            spriteMusicToggle.sprite = spriteMusicOn;
        }
        else if(AudioListener.volume == 1.0f) {
            AudioListener.volume = 0.0f;

            spriteMusicToggle.sprite = spriteMusicOff;
        }
    }
}

Give that class a Start() function and then set the correct sprite based on the extant variable being on the volume.

1 Like

Hello! Thank you so much for the answer, but I am practically nil at coding: would it be something like this:

void Start()
{
target = imagesource;
}

Hey did you fix this? I am also having challenge using this script…