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. Dismiss Notice

Question I'm having trouble keeping the saved sound values.

Discussion in 'Scripting' started by sergiogarciavicooficial, Jul 9, 2023.

  1. sergiogarciavicooficial

    sergiogarciavicooficial

    Joined:
    Nov 30, 2021
    Posts:
    3
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Audio;
    5. using UnityEngine.UI;
    6.  
    7. public class Volumen : MonoBehaviour
    8. {
    9.     [SerializeField] private AudioMixer myMixer;
    10.     [SerializeField] private Slider sliderSFX;
    11.     [SerializeField] private Slider sliderBGM;
    12.     [SerializeField] private Image imagenMuteBGM;
    13.     [SerializeField] private Image imagenMuteSFX;
    14.     [SerializeField] private Image imagenLowVolumeSFX;
    15.     [SerializeField] private Image imagenNormalVolumeSFX;
    16.     [SerializeField] private Image imagenLowVolumeBGM;
    17.     [SerializeField] private Image imagenNormalVolumeBGM;
    18.  
    19.     private void Awake()
    20.     {
    21.         if (PlayerPrefs.HasKey("SFX"))
    22.         {
    23.             LoadSFXVolumen();
    24.         }
    25.         else
    26.         {
    27.             SetSFXVolume();
    28.         }
    29.  
    30.         if (PlayerPrefs.HasKey("BGM"))
    31.         {
    32.             LoadBGMVolume();
    33.         }
    34.         else
    35.         {
    36.             SetBGMVolume();
    37.         }
    38.         EstoyMuted();
    39.     }
    40.  
    41.     public void SetSFXVolume()
    42.     {
    43.         float volumeSFX = sliderSFX.value;
    44.         myMixer.SetFloat("SFX", Mathf.Log10(volumeSFX) * 20);
    45.         PlayerPrefs.SetFloat("SFX", sliderSFX.value);
    46.         EstoyMuted();
    47.         PlayerPrefs.Save();
    48.     }
    49.  
    50.     public void SetBGMVolume()
    51.     {
    52.         float volumeBGM = sliderBGM.value;
    53.         myMixer.SetFloat("BGM", Mathf.Log10(volumeBGM) * 20);
    54.         PlayerPrefs.SetFloat("BGM", sliderBGM.value);
    55.         PlayerPrefs.Save();
    56.         EstoyMuted();
    57.     }
    58.  
    59.     private void LoadSFXVolumen()
    60.     {
    61.         sliderSFX.value = PlayerPrefs.GetFloat("SFX");
    62.         SetSFXVolume();
    63.     }
    64.  
    65.     private void LoadBGMVolume()
    66.     {
    67.         sliderBGM.value = PlayerPrefs.GetFloat("BGM");
    68.         SetBGMVolume();
    69.     }
    70.  
    71.     public void EstoyMuted()
    72.     {
    73.         myMixer.GetFloat("SFX", out float volumeSFX);
    74.         myMixer.GetFloat("BGM", out float volumeBGM);
    75.  
    76.         imagenMuteSFX.enabled = false;
    77.         imagenLowVolumeSFX.enabled = false;
    78.         imagenNormalVolumeSFX.enabled = false;
    79.  
    80.         if (volumeSFX <= -80f)
    81.         {
    82.             imagenMuteSFX.enabled = true;
    83.         }
    84.         else if (volumeSFX <= -6f)
    85.         {
    86.             imagenLowVolumeSFX.enabled = true;
    87.         }
    88.         else
    89.         {
    90.             imagenNormalVolumeSFX.enabled = true;
    91.         }
    92.  
    93.         imagenMuteBGM.enabled = false;
    94.         imagenLowVolumeBGM.enabled = false;
    95.         imagenNormalVolumeBGM.enabled = false;
    96.  
    97.         if (volumeBGM <= -80f)
    98.         {
    99.             imagenMuteBGM.enabled = true;
    100.         }
    101.         else if (volumeBGM <= -6f)
    102.         {
    103.             imagenLowVolumeBGM.enabled = true;
    104.         }
    105.         else
    106.         {
    107.             imagenNormalVolumeBGM.enabled = true;
    108.         }
    109.     }
    110.  
    111.  
    112. }
    I've been trying for a while, but I can't find the error. I managed to make the graphics options work, etc. But sound is the only one that doesn't save the values for me.
     
  2. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    449
    Your prefs are saved in those SetXXXVolume methods, but when are you calling them? I see you're calling them in the Load methods (can't see why, by the way), and those are called on Awake().

    I suppose you want to save your player's volume choices at some point? Like triggered by the volume slider change? Or on Application.exit?
     
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,495
    Yes, that's the important bit that is missing. He might be calling them from UI callbacks which is what I would guess. However the question is if those has been setup correctly. Some Debug.Logs might shed some light onto those issues. However an actual explanation how it is supposed to work would be great. That should have been part of the question.
     
  4. sergiogarciavicooficial

    sergiogarciavicooficial

    Joined:
    Nov 30, 2021
    Posts:
    3
    If I want to constantly save the settings while the player interacts with the Slider.
    upload_2023-7-10_14-31-39.png
    That is a slider that calls one of the Set functions every time it interacts with it, and I want it to constantly update the PlayerPrefs for SFX. Then, when I re-enter the game, it should load those saved sound PlayerPrefs.
     
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,495
    Are you sure you actually linked the "correct" object in that UnityEvent? Keep in mind that linking a prefab is possible but would not do what you want. You can actually call methods on prefabs. Make sure your slider event is actually linked to the actual instance in the scene. You can simply click on the ObjectField and Unity will highlight the linked object.

    As I said above, try adding a Debug.Log line like this to your set method:

    Code (CSharp):
    1. Debug.Log("SetSFXVolume start", gameObject);
    Place that code at the very top of the method and this one at the bottom of the same method

    Code (CSharp):
    1. Debug.Log("SetSFXVolume new value: " + volumeSFX, gameObject);
    You can click on the message in the console to see on which object this code run on (thanks to the second argument). I recommend two logs in case there's an error in between.
     
  6. sergiogarciavicooficial

    sergiogarciavicooficial

    Joined:
    Nov 30, 2021
    Posts:
    3
    I just solved it, thanks a lot for the help.
     
    KillDashNine likes this.