Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

saving a toggle state for muting?

Discussion in 'Scripting' started by lewislepton, Apr 3, 2015.

  1. lewislepton

    lewislepton

    Joined:
    Mar 25, 2015
    Posts:
    34
    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

    Code (CSharp):
    1. public bool isMute;
    2.  
    3.     public void MuteToggle(){
    4.         isMute = !isMute;
    5.         if (isMute == true) {
    6.             AudioListener.volume = 0;
    7.         } else {
    8.             AudioListener.volume = 1;
    9.         }
    10.     }
     
  2. L-Tyrosine

    L-Tyrosine

    Joined:
    Apr 27, 2011
    Posts:
    305
    isMute value is lost when the owner behaviour is destroyed. You could use static or dont use a second value

    Code (csharp):
    1.  
    2. public bool IsMute
    3. {
    4.     get
    5.     {
    6.         return AudioListener.volume == 0;
    7.     }
    8.  
    9.     set
    10.     {
    11.         AudioListener.volume = value ? 0 : 1;
    12.     }
    13. }
    14.  
     
  3. lewislepton

    lewislepton

    Joined:
    Mar 25, 2015
    Posts:
    34
    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
     
  4. lewislepton

    lewislepton

    Joined:
    Mar 25, 2015
    Posts:
    34
    aye, im just not getting it im sorry. its all a little bit 'hhhmmmmmm' to me
     
  5. L-Tyrosine

    L-Tyrosine

    Joined:
    Apr 27, 2011
    Posts:
    305
    No problem. What is your programming experience/background?
     
  6. lewislepton

    lewislepton

    Joined:
    Mar 25, 2015
    Posts:
    34
    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
     
  7. lewislepton

    lewislepton

    Joined:
    Mar 25, 2015
    Posts:
    34
    but what i would have thought is using say:
    Code (CSharp):
    1. void Awake(){
    2. DontDestroyOnLoad(this.gameObject);
    3. }
    may have worked since its calling to not be destroyed. but guess its not the way i would have thought so
     
  8. L-Tyrosine

    L-Tyrosine

    Joined:
    Apr 27, 2011
    Posts:
    305
    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?
     
  9. lewislepton

    lewislepton

    Joined:
    Mar 25, 2015
    Posts:
    34
    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 ;)

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

    lewislepton

    Joined:
    Mar 25, 2015
    Posts:
    34
    after some much needed sleep im ready to nail this down ;)
     
  11. lewislepton

    lewislepton

    Joined:
    Mar 25, 2015
    Posts:
    34
    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
     
  12. lewislepton

    lewislepton

    Joined:
    Mar 25, 2015
    Posts:
    34
    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 ;)
     
  13. lewislepton

    lewislepton

    Joined:
    Mar 25, 2015
    Posts:
    34
    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 ;)
     
  14. Jami_2

    Jami_2

    Joined:
    Apr 22, 2013
    Posts:
    19
    I use this options for that works for me.

    Code (CSharp):
    1. public bool isMute;
    2.  
    3. Int float volume;
    4. void Start ()
    5. {
    6. PlayerPrefs.GetFloat ("music");
    7. }
    8.  
    9.     public void MuteToggle(){
    10.         isMute = !isMute;
    11.         if (isMute == true) {
    12.             AudioListener.volume = 0;
    13.            PlayerPrefs.SetFloat ("music", 0);
    14.         } else {
    15.             AudioListener.volume = 1;
    16. PlayerPrefs.SetFloat ("music", 1);
    17.         }
    18.     }
    Cannot promise thats Compile. Im not on my pc.

    The general idea is Save your State in Playerprefs.
     
  15. lewislepton

    lewislepton

    Joined:
    Mar 25, 2015
    Posts:
    34
    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
     
  16. lewislepton

    lewislepton

    Joined:
    Mar 25, 2015
    Posts:
    34
    though on seeing this, your 3rd line reads "Int float volume;"
    im more than sure that is typo ;)
     
  17. Jami_2

    Jami_2

    Joined:
    Apr 22, 2013
    Posts:
    19
    Yes it is. I write it by my phone.:)
     
  18. lewislepton

    lewislepton

    Joined:
    Mar 25, 2015
    Posts:
    34
    no worries. but thanks again ;)