saving a toggle state for muting?

i have just done a nice mute for music, which im happy about. but now i really want to make sure that when my scene resets for each game, then mute does not revert back to its off state.

here is my current music mute code, which works perfectly. just what i need. but can’t get the state of the toggle to stay as it should when resetting the scene.

have tried a ‘DontDestroyOnLoad’ for just the toggle, but not working. weird.

thanks to anyone who can help

public bool isMute;

    public void MuteToggle(){
        isMute = !isMute;
        if (isMute == true) {
            AudioListener.volume = 0;
        } else {
            AudioListener.volume = 1;
        }
    }

isMute value is lost when the owner behaviour is destroyed. You could use static or dont use a second value

public bool IsMute
{
    get
    {
        return AudioListener.volume == 0;
    }
  
    set
    {
        AudioListener.volume = value ? 0 : 1;
    }
}

oooh thanks.

but im slightly confused as to how his would incorporate into the mute code from what i posted.

what i also have set up is a separate toggle state for the toggle mute itself. but really do not know where this code you have posted would go. i have played with it, but really am not getting anything.

thank you

aye, im just not getting it im sorry. its all a little bit ‘hhhmmmmmm’ to me

No problem. What is your programming experience/background?

a mixture of everything. though have started to just nail on unity.
had down lua, c++, some java/JS. though unity has been the first that i have really wanted to now stay in lately

but what i would have thought is using say:

void Awake(){
DontDestroyOnLoad(this.gameObject);
}

may have worked since its calling to not be destroyed. but guess its not the way i would have thought so

This will make the game object live between scenes; Yes the value should keep for the owner script is not being destroyed… Where and how MuteToggle is called?

the mute toggle shows when the player dies. a canvas pops up in the same scene, this holds things like a quit button, which takes you back to the menu. also a retry button which loads the level again, then has a mute button which is the culprit in question :wink:

there are only 2 scenes in the whole game so far. but this will more than likely stay that way for a while

after some much needed sleep im ready to nail this down :wink:

can anybody help on this please.
i am having the worst trouble getting it to work, and really this is the last piece of the puzzle. so any help that helps fix this would be great.

thank you

bump…

can nobody help me on this please. im having just a super hard time figuring this out.

again, all i need is help to stop my toggle from resetting position states. so if i hit mute, when i reload the level, the toggle is staying down. then if i hit unmute, it stays up.

i have the music code working perfectly. just this toggle is my last thing. I’ve been up since 8am trying to get this.

thank you to anyone who can help, even an example would be good to just learn from

again, thank you to anyone who can put this to bed :wink:

just to clarify where things are.

i have 2 scenes just now. there will be more, but this is just an initial thing.
but there is just a menu, then the game screen. the audio stuff, including music, sound and scripts are loaded in the game scene, since you will be that part the most.
when you die, a canvas pops up with a retry button, quit, mute sound and share screenshot.
the script for the sound is located in a game object which handles all my sound. i have another object which handles other non-audio things. such as restarting levels etc

this would make my day, and future games if this can be helped with :wink:

I use this options for that works for me.

public bool isMute;

Int float volume;
void Start ()
{
PlayerPrefs.GetFloat ("music");
}

    public void MuteToggle(){
        isMute = !isMute;
        if (isMute == true) {
            AudioListener.volume = 0;
           PlayerPrefs.SetFloat ("music", 0);
        } else {
            AudioListener.volume = 1;
PlayerPrefs.SetFloat ("music", 1);
        }
    }

Cannot promise thats Compile. Im not on my pc.

The general idea is Save your State in Playerprefs.

oh wow. thanks, ill have a deep look at that, see what turns up.

i did decide for now to incorporate a touch mute method just now, which works. but once i get this method down, then will replace it.

again, thanks

though on seeing this, your 3rd line reads “Int float volume;”
im more than sure that is typo :wink:

Yes it is. I write it by my phone.:slight_smile:

no worries. but thanks again :wink: