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

UI Audio slider get lost when changing scene

Discussion in 'Scripting' started by Deleted User, Mar 4, 2018.

  1. Deleted User

    Deleted User

    Guest

    hi, in menu scene i have a gameobject called audiomanager and it has following script attached to. i have 2 audio clips. i need to play clip1 in menu scene and level scene. then i need to play clip2 in gameplay scene.

    now problem is if i change scene and come back to menu scene nothing works correctly, neither sliders...nor toggle audio . how can i fix that?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AudioManager : MonoBehaviour
    6. {
    7.     public static AudioManager Instance;
    8.  
    9.  
    10.     void Awake()
    11.     {
    12.         GameObject[] objs = GameObject.FindGameObjectsWithTag ("bgmusic");
    13.         if (objs.Length > 1)
    14.             Destroy (this.gameObject);
    15.  
    16.  
    17.         DontDestroyOnLoad(this.gameObject);
    18.     }
    19. }
    20.  
     
    Last edited by a moderator: Mar 4, 2018
  2. Deleted User

    Deleted User

    Guest

    i used this and its ok now. but how will you save audio volume after game is restarted?

    Code (CSharp):
    1. public static AudioManager Instance;
    2.  
    3.     void Awake()
    4.     {
    5.         GameObject[] objs = GameObject.FindGameObjectsWithTag ("bgmusic");
    6.         if (objs.Length > 1)
    7.             Destroy (this.gameObject);
    8.  
    9.  
    10.         DontDestroyOnLoad(this.gameObject);
    11.     }
    12.  
    13.     void Update()
    14.     {
    15.         if (SceneManager.GetActiveScene().name == "GamePlaySce1")
    16.         {
    17.             Destroy(this.gameObject);
    18.         }
    19.     }
     
  3. carl010010

    carl010010

    Joined:
    Jul 14, 2010
    Posts:
    139
  4. Deleted User

    Deleted User

    Guest

    can you say me how to save toggle button sprites?
     
  5. carl010010

    carl010010

    Joined:
    Jul 14, 2010
    Posts:
    139
  6. Deleted User

    Deleted User

    Guest

    i have seen alot of them since 3 hours... i just need a script snippet.
     
  7. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    PlayerPrefs.SetBool("toggleButton", toggle.isOn);
     
    SparrowGS and Deleted User like this.
  8. Deleted User

    Deleted User

    Guest

    Code (CSharp):
    1.     private void Start()
    2.     {
    3.         source = GetComponent<AudioSource>();
    4.  
    5.         if (PlayerPrefs.HasKey("Volume"))
    6.         {
    7.             source.volume = PlayerPrefs.GetFloat("Volume");
    8.         }
    9.     }
    10.  
    11.     public void Mute()
    12.     {
    13.         mute = !mute;
    14.         if (mute)
    15.         {
    16.             source.volume = 0;
    17.         }
    18.         else
    19.         {
    20.             source.volume = 1;
    21.         }
    22.         PlayerPrefs.SetFloat("Volume", source.volume);
    23.     }
    this code saves toggle between game run. but still ui button sprites stay without change.
    i meant how to say this sprites condition not that sorry...lol
     
  9. Deleted User

    Deleted User

    Guest

    i use this , but still does not work
    Code (CSharp):
    1. public static AudioManager Instance;
    2.  
    3.     private bool mute;
    4.     private AudioSource source;
    5.  
    6.     public Button buttontoggle;
    7.     public Sprite musicOn;
    8.     public Sprite musicOff;
    9.  
    10.     void Awake()
    11.     {
    12.         GameObject[] objs = GameObject.FindGameObjectsWithTag ("bgmusic");
    13.         if (objs.Length > 1)
    14.             Destroy (this.gameObject);
    15.  
    16.  
    17.         DontDestroyOnLoad(this.gameObject);
    18.     }
    19.  
    20.     void Update()
    21.     {
    22.         if (SceneManager.GetActiveScene().name == "GamePlaySce1")
    23.         {
    24.             Destroy(this.gameObject);
    25.         }
    26.     }
    27.  
    28.  
    29.     private void Start()
    30.     {
    31.         source = GetComponent<AudioSource>();
    32.  
    33.         if (PlayerPrefs.HasKey("Volume"))
    34.         {
    35.             source.volume = PlayerPrefs.GetFloat("Volume");
    36.         }
    37.  
    38.  
    39.         mute = false;
    40.         if (source.volume == 1)
    41.         {
    42.             buttontoggle.image.sprite = musicOn;
    43.         }
    44.         else
    45.         {
    46.             buttontoggle.image.sprite = musicOff;
    47.         }
    48.     }
    49.  
    50.     public void Mute()
    51.     {
    52.         mute = !mute;
    53.         if (mute)
    54.         {
    55.             source.volume = 0;
    56.         }
    57.         else
    58.         {
    59.             source.volume = 1;
    60.         }
    61.         PlayerPrefs.SetFloat("Volume", source.volume);
    62.     }
     
  10. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    if (source.volume == 1) should probably be
    if(source.volume > 0)
    I would think. I don't know what you are doing and not much about audio.
     
    Deleted User likes this.
  11. Deleted User

    Deleted User

    Guest

    i want to save toggle button sprites. when music is muted , we have one sprite on toggle button. and when music is unmuted , the toggle button has another sprite.
    now in this script , toggle music will be saved. but its swapping sprite does not. i try to write code for that option.
     
  12. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Code (csharp):
    1.  
    2. [LIST=1]
    3. [*]  
    4. [*]        if (source.volume == 1)
    5. [*]        {
    6. [*]            buttontoggle.image.sprite = musicOn;
    7. [*]          Debug.Log("sprite changed to musicOn");
    8. [*]        }
    9. [*]        else
    10. [*]        {
    11. [*]            buttontoggle.image.sprite = musicOff;
    12. [*]          Debug.Log("sprite changed to musicOff");
    13. [*]        }
    14. [/LIST]
    15.  
    If nothing is printed, then it didn't recognize your volume. Try that.
     
    Deleted User likes this.
  13. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    The trouble is, the volume could be anything from 0 to 1, so you are saving it to full blast. You need to save the volume separetly
    Code (csharp):
    1.  
    2. PlayerPrefs.SetFloat("musicVolume", audio.volume);
    3. if(audio.volume) > 0){
    4. PlayerPrefs.SetBool("musicToggle", true);
    5. }
    6.  
    7. if(PlayerPrefs.GetBool("musicToggle" == true){
    8. //set sprite to music on
    9. }
    10. else...
    11.  
    or simpler
    Code (csharp):
    1.  
    2. if(PlayerPrefs.GetFloat("musicVolume")>0{
    3. //setSprite to music on
    4. }
    5.  
     
    Deleted User likes this.
  14. Deleted User

    Deleted User

    Guest

    should i put your code in AudioManager script?
    and then how to connect them in editor?
     
  15. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    If you have the audio source reference it doesn't matter what script it is in, but you probably want to do all your audio saves in one place. Retrieving the saved value, that can be anywhere.
     
  16. Deleted User

    Deleted User

    Guest

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6.  
    7. public class AudioManager : MonoBehaviour
    8. {
    9.     public static AudioManager Instance;
    10.     private bool mute;
    11.     private AudioSource source;
    12.  
    13.     public Slider volumeSlider;
    14.  
    15.     void Awake()
    16.     {
    17.         GameObject[] objs = GameObject.FindGameObjectsWithTag ("bgmusic");
    18.         if (objs.Length > 1)
    19.             Destroy (this.gameObject);
    20.  
    21.         DontDestroyOnLoad(this.gameObject);
    22.     }
    23.     void Update()
    24.     {
    25.         if (SceneManager.GetActiveScene().name == "GamePlaySce1")
    26.         {
    27.             Destroy(this.gameObject);
    28.         }
    29.     }
    30.     private void Start()
    31.     {
    32.         source = GetComponent<AudioSource>();
    33.  
    34.         source.volume = PlayerPrefs.GetFloat("Volume");
    35.  
    36.         volumeSlider.value = PlayerPrefs.GetFloat("volumeslider");
    37.    
    38.     }
    39.     public void Mute()
    40.     {
    41.         mute = !mute;
    42.         if (mute)
    43.         {
    44.             source.volume = 0;
    45.         }
    46.         else
    47.         {
    48.             source.volume = 1;
    49.         }
    50.         PlayerPrefs.SetFloat("Volume", source.volume);
    51.     }
    52.  
    53.     public void VolumeSlider()
    54.     {
    55.        
    56.        
    57.         PlayerPrefs.SetFloat("volumeslider", volumeSlider.value);
    58.     }
    59. }
    60.  
    with this script audio slider value is saved, but when i drag it to adjust the audio volume, it does not work. what is missing there? what code should be written in VolumeSlider() ?
     
    Last edited by a moderator: Mar 5, 2018
  17. Deleted User

    Deleted User

    Guest

    when changing scene , slider will miss . after returning back to menu scene, inspector shows none slider gameobject. how to say to script that i have only in one scene slider?

    Code (CSharp):
    1. sing System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6.  
    7. public class AudioManager : MonoBehaviour
    8. {
    9.     public static AudioManager Instance;
    10.  
    11.     public AudioSource source;
    12.  
    13.     public Slider volumeSlider;
    14.  
    15.     void Awake()
    16.     {
    17.         GameObject[] objs = GameObject.FindGameObjectsWithTag ("bgmusic");
    18.         if (objs.Length > 1)
    19.             Destroy (this.gameObject);
    20.  
    21.         DontDestroyOnLoad(this.gameObject);
    22.     }
    23.     void Update()
    24.     {
    25.      
    26.  
    27.  
    28.         source.volume = PlayerPrefs.GetFloat("volumeslider");
    29.     }
    30.  
    31.     private void Start()
    32.     {
    33.  
    34.  
    35.         volumeSlider.value = PlayerPrefs.GetFloat("volumeslider");
    36.  
    37.     }
    38.  
    39.  
    40.     public void VolumeSlider()
    41.     {
    42.      
    43.      
    44.         PlayerPrefs.SetFloat("volumeslider", volumeSlider.value);
    45.     }
    46. }
    47.  
     
    Last edited by a moderator: Mar 5, 2018
  18. Deleted User

    Deleted User

    Guest

    i could not find any answer. then i created in each scene one audio source.

    but how can you save mute audio toggle button?
     
  19. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    If your latest question is how to save a toggle value, you can store that as a bool and load its value in Start and set the toggle based on the loaded value.
     
    Deleted User likes this.
  20. Deleted User

    Deleted User

    Guest

    hi and thanks for your reply. i could finally make that ok with youtube tutorial. but this is my question :
    in menu scene i have singleton and stores and saves music. then when i go to level scene still music continues without problem. but as i go to play scene , i destroy singleton. because in game scene i have a third music . the problem is here. now when i go back to menu scene , music is not controlled with slider and every thing is lost in inspector. why?
    i hope my question is clear.
     
  21. Deleted User

    Deleted User

    Guest

    actually i mean , in each unity scene i have a different audio . how can i adjust or change general level of game audio only with one slider , which is in menu scene. i need this answer . thanks.
     
  22. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    If you can only change the volume on the menu scene, what you can do is put a script in each scene that references the audio sources and in Start(), load the volume and assign it to the sources.

    You can have 2 values (or more), for say: music, and sound effects. Then make 2 lists/arrays, as variables on the script, and drag the audio source(s) to the appropriate category/type.
     
    Deleted User likes this.
  23. Deleted User

    Deleted User

    Guest

    there are three audio sources. each one in 1 scene.
    but slider is only in menu scene.
    so when i come to second scene , maybe audio to be lower or higher .
    i need current amount of slider to be applied to all musics.
    if you give me snipptet it helps. thanks,
     
  24. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    What I was thinking was something like this...
    Code (csharp):
    1. public class AudioAdjust : MonoBehaviour {
    2.  
    3. [SerializeField]
    4. List<AudioSource> effects;
    5. [SerializeField]
    6. List<AudioSource> music;
    7.  
    8. void Start() {
    9.    float musicvol = PlayerPrefs.GetFloat("MusicVolume");
    10.    float effectvol = PlayerPrefs.GetFloat("EffectVolume");
    11.  
    12.    for(int i = 0; i < effects.Count; ++i) effects[i].volume = effectvol;
    13.    for(int i = 0; i < music.Count; ++i) music[i].volume = musicvol;
    14.  }
    15. }
    You could have just 1 per scene .. you could have more, too, either way.
    Drag all the sound effect sources into that list, and the music ones to the music list.

    Now, your menu scene will change the volume and save it to player prefs. These scripts will read back the volume and assign it to each source(/type). :)
     
    Deleted User likes this.
  25. Deleted User

    Deleted User

    Guest

    let me try that ! lol but thanks :)
     
  26. Deleted User

    Deleted User

    Guest

    aha , i have already this script in menu scene for audio things,,,

    does your script will make my script not working? what i do ? i mix them or just create that and these will talk together?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6.  
    7. public class AudioManager : MonoBehaviour
    8. {
    9.     public AudioSource source;
    10.     public Slider volumeSlider;
    11.  
    12.     public GameObject audioOnIcon;
    13.     public GameObject audioOffIcon;
    14.  
    15.  
    16.     void Update()
    17.     {
    18.         source.volume = PlayerPrefs.GetFloat("volumeslider");
    19.     }
    20.  
    21.     private void Start()
    22.     {
    23.         SetSoundStates ();
    24.  
    25.         volumeSlider.value = PlayerPrefs.GetFloat("volumeslider");
    26.     }
    27.     public void VolumeSlider()
    28.     {
    29.         PlayerPrefs.SetFloat("volumeslider", volumeSlider.value);
    30.     }
    31.  
    32.     public void ToggleSound()
    33.     {
    34.         if (PlayerPrefs.GetInt ("MutedSounds", 0) == 0)
    35.             PlayerPrefs.SetInt ("MutedSounds", 1);
    36.         else
    37.         {
    38.             PlayerPrefs.SetInt ("MutedSounds", 0);
    39.  
    40.         }
    41.         SetSoundStates ();
    42.     }
    43.     private void SetSoundStates()
    44.     {
    45.         if (PlayerPrefs.GetInt ("MutedSounds", 0) == 0)
    46.         {
    47.             AudioListener.volume = 1;
    48.             audioOnIcon.SetActive (true);
    49.             audioOffIcon.SetActive (false);
    50.         }
    51.         else
    52.         {
    53.             AudioListener.volume = 0;
    54.             audioOnIcon.SetActive (false);
    55.             audioOffIcon.SetActive (true);
    56.         }
    57.     }
    58.  
    59. }
    60.  
     
  27. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I don't think your script will bother the script I wrote at all. Remember, the script I wrote is for other scenes, not the menu. If you have any sound or effects on the menu scene, I would suggest that you take care of that in the script there (or some other script in the menu)..

    Just try a quick test .. go to just 1 game scene of yours, add the script I wrote and put 1 music / effect audio source into the correct spot, and run the game. Go to the menu, change whichever volume type you added for the test, and then load the other scene and check that the volume is correct. :)
     
    Deleted User likes this.
  28. Deleted User

    Deleted User

    Guest

    all right . correct. i do that . :)
     
  29. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Then, if that works.. fix the 'source.volume' in the script that you posted, so you just update that when you change the volume and delete your update loop. There is no reason to set the source volume every frame, by fetching the player prefs value ;)
     
    Deleted User likes this.
  30. Deleted User

    Deleted User

    Guest

    ok . in level scene i created an empty gameobject. assigned your script to that. dragged then audio source to that gameobject. and running game , but volume is 0 as soon as game runs. going to menu scene and changing slider , does not do anything for that to be ok. what i am missing now?
     
  31. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Is this code in your script running, on the menu scene?
    Code (csharp):
    1. public void VolumeSlider()
    2. {
    3.     Debug.Log("Set volume");
    4.     PlayerPrefs.SetFloat("volumeslider", volumeSlider.value);
    5.   /* maybe add this */
    6.     PlayerPrefs.Save();
    7. }
    Try that, check the log.. make sure it's running.. add the line to save, perhaps? Try again.
     
    Deleted User likes this.
  32. Deleted User

    Deleted User

    Guest

    Code (CSharp):
    1. Set volume
    2. UnityEngine.Debug:Log(Object)
    3. AudioManager:VolumeSlider() (at Assets/Scripts/AudioManager.cs:30)
    4. UnityEngine.UI.Slider:set_value(Single)
    5. AudioManager:Start() (at Assets/Scripts/AudioManager.cs:25)
    6.  
    yes , it is running as you see.
    if i remove that from Update() , i talk on my script, then slider does not work.
    by your script still i cannot use it correctly ....
     
  33. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Don't worry about the Update change for now.. it will work if you move the source volume to when you set the slider, instead of Update, but let's focus on 1 thing at a time..

    I do not really understand what is not working. Do you get any errors?
     
    Deleted User likes this.
  34. Deleted User

    Deleted User

    Guest

    when i run level scene , audio source volume parameter gets 0 .
    if i go to level scene from menu scene , new scene is loaded, menu scene things are unloaded , but no music can be heard, in spite of that gameobject and its audio source and your script . in console there is no error.what is that meaning then?
     
  35. Deleted User

    Deleted User

    Guest

    so base on what methos5k said, in level scene i created a new gameobject , attached his script to that and changed it so that it reads from slider from another script . now these 2 scripts communicate to each other and music is all over game scenes at the same level. this is final script. thank you. :)
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6.  
    7. public class AudioAdjust : MonoBehaviour
    8. {
    9.  
    10.     public AudioSource music;
    11.  
    12.     void Start()
    13.     {
    14.         music.volume = PlayerPrefs.GetFloat("volumeslider");
    15.     }
    16. }
    17.  
     
  36. Deleted User

    Deleted User

    Guest

    by the way what is difference between music slider and fx slider. i have already music slider . if i want to have fx slider in order to make settings box more filled with things, how can i apply this code side to that ? i know fx slider makes small audio fx controll ... but how to code that?
     
  37. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Hm. I'm glad you got it working. The only difference between my script and yours is that my script could handle more than 1 audio source to adjust.. if you had more than 1 in the scene.

    While you were figuring that out, I had put together a tiny package to help you because i thought you were confused. :)

    I may as well add it here , since it's done. lol
     

    Attached Files:

    Deleted User likes this.
  38. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I'm not sure if you're talking about 'fx' , in the sense that I had written effect or something else.

    In my example, I meant to distinguish between sound effects.. like "Bing, bang, bong, clap" and the like, compared to actual music. :) The user may wish to have different volume levels for them individually.
     
    Deleted User likes this.
  39. Deleted User

    Deleted User

    Guest

    yes. fist of all thaaaaaaks for your package . that will be tutorial like , i will see that.
    that is exactly what i mean.Bing, bang, bong, clap ..
    how can man to have another extra slider for sliderFX? can you please give help about that too?
     
  40. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, nothing to it.. just add another slider? :)

    In my tiny unity package, there are a couple of audio sources in the menu scene (effects and music) and a couple in the other scene.. Modifying 1 slider will adjust the effects/music in both scenes, and the other slider.. the other ones.

    It's very basic.. there is of course no music or sound effects - just the audio sources, but you can see their 'volume' values, which would be the same if you actually had content to play as sound ;)
     
    Deleted User likes this.
  41. Deleted User

    Deleted User

    Guest

    Wonderful. i think my game project sound works is done . i searched today totally around music workflow in unity game engine ... from forum ...answers...youtube... so your package will be for anyone else useful. thanks for your attention to new programmers. Good luck .

    by the way , how in forum can man replay to someone ... and make his or her name blue .. to correspond to him...her...some forum things...
    and if i have new question ... if i open new thread ,, will it make non good sense situation for all other developers...here?
    but generally thanks and specifically thanks to you. :)
     
  42. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You just type '@' and start typing their name, you will see a popup and you can click the user's name.

    I do not understand this part of your question: "and if i have new question ... if i open new thread ,, will it make non good sense situation for all other developers...here?"

    Anyways, you're welcome & good luck with your game :)
     
  43. Deleted User

    Deleted User

    Guest

    hehhhhhh lol ... big lol :)
    thanks!