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

Toggle audio group på UI Toggle

Discussion in 'Editor & General Support' started by Reject76, Jan 16, 2019.

  1. Reject76

    Reject76

    Joined:
    Jun 12, 2015
    Posts:
    38
    Hello

    I've been searching high and low, both on google and this forum and I cant believe that I cant find the answer to this;

    Surely it must be possible to use a UI - Toggle to mute/unmute an audio group? Just I cant for the life of me find out how...

    Help me Unity community...you're my only hope....
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,930
  3. Reject76

    Reject76

    Joined:
    Jun 12, 2015
    Posts:
    38
    Many thanks for the reply, Lurking-Ninja, much obliged.

    I kinda got things going, albeit in a different way. I have managed to setup two Toggles that will mute and unmute selected audio groups. However, I'm having troubles getting them to save their state to PlayerPrefs independantly of each other.

    See my script below; if I comment out anything with FX the music toggle will work and save state as intended. Same if I comment out anything with music, then the FX will work as intended. But if I leave the code as is below, then it seems they inherit each others state. Utterly annoying for what should be a simple issue. Any ideas?

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7.  
    8. public class MainMenu : MonoBehaviour
    9. {
    10.  
    11.     //gameobjects are assigned by drag-and-drop in the inspector
    12.     public Toggle fxtogglebtn;
    13.     public Toggle musictogglebtn;
    14.  
    15.     public bool fxbool;
    16.     public bool musicbool;
    17.  
    18.     string fxboolstring;
    19.     string musicboolstring;
    20.  
    21.     // Start is called before the first frame update
    22.     void Start()
    23.     {
    24.  
    25.         fxbool = (PlayerPrefs.GetInt(fxboolstring) != 0);
    26.         musicbool = (PlayerPrefs.GetInt(musicboolstring) != 0);
    27.      
    28.         fxtogglebtn.isOn = fxbool;
    29.         musictogglebtn.isOn = musicbool;
    30.      
    31.     }
    32.  
    33.  
    34.  
    35.     public void savefxbool()
    36.     {
    37.  
    38.         if (fxtogglebtn.isOn == true)
    39.         {
    40.  
    41.             PlayerPrefs.SetInt(fxboolstring, (true ? 1 : 0));
    42.  
    43.         }
    44.  
    45.         if (fxtogglebtn.isOn == false)
    46.         {
    47.  
    48.             PlayerPrefs.SetInt(fxboolstring, (false ? 1 : 0));
    49.  
    50.         }
    51.  
    52.  
    53.  
    54.     }
    55.  
    56.     public void savemusicbool()
    57.     {
    58.  
    59.         if (musictogglebtn.isOn == true)
    60.         {
    61.  
    62.             PlayerPrefs.SetInt(musicboolstring, (true ? 1 : 0));
    63.  
    64.         }
    65.  
    66.         if (musictogglebtn.isOn == false)
    67.         {
    68.  
    69.             PlayerPrefs.SetInt(musicboolstring, (false ? 1 : 0));
    70.  
    71.         }
    72.  
    73.     }
    74.  
    75.  
    76.  
    77. }
    78.  
     
  4. Reject76

    Reject76

    Joined:
    Jun 12, 2015
    Posts:
    38
    Bumping this thread just once. Really cant figure out why my FX routines and Music routines work independantly of each other, but not when both are active at the same time...
     
  5. Reject76

    Reject76

    Joined:
    Jun 12, 2015
    Posts:
    38
    Last edited: Jan 19, 2019
    Lurking-Ninja likes this.