So I found a question with the same problem as me, but sadly it wasn’t answered. Anyone can help please?
Basically the sliders that modify the Music and the Audio FXs work when I play in the editor mode in my computer, but not when building for android. I create the exposed parameters in the Audio Mixers that control the volume etc. As I said, it works when I hit play button, but not at all in my phone. This is the code:
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.UI;
public class VolumeSliders : MonoBehaviour
{
public AudioMixer mixer;
[SerializeField] private string audioMixerParameterName;//I create a PlayerPrefs float with the same Key
private Slider slider;
private void Awake()
{
slider = GetComponent<Slider>();
}
private void Start()
{
slider.value = PlayerPrefs.GetFloat(audioMixerParameterName, 1f);
}
public void SetVolume(Slider slider)
{
mixer.SetFloat(audioMixerParameterName, Mathf.Log10(slider.value) * 20);
PlayerPrefs.SetFloat(audioMixerParameterName, slider.value);
PlayerPrefs.Save();
}
}
Is there any fix or workaround this issue?
Thank you
You never call SetVolume in your script, but I assume you call it somewhere else, right? Can you make sure SetVolume is called even on android? What version of Unity are you working on? Are you on the latest LTS? Because if you are not and this is a bug, there is a chance it was already fixed.
For mixer.SetFloat to be set, you need to call the function SetVolume somewhere. You probably need to add a reference to your script on your slider so that when OnValueChanged happens, it calls SetVolume like this. On the same note, you have two variables named slider, which can be confusing. You should remove the Slider slider inside SetVolume if you are getting it in the Awake
Ohh yeah, sorry I misunderstood. I forgot SetVolume was my method, I thought it was a bult in Unity one. Yes of course I call that function exactly as you showed me. As I said, it works perfectly fine in Editor mode (I think this is important to notice). I tried what you suggested, updating to Unity 2021 LTS, and removing audio mixer as a parameter as I call it already in Awake, but nothing, the problem still persists, basically when I build it for android it stops working.
Do you know any workaround or other way of implementing a volume slider with AudioMixer? Something else that I may try? Thanks!
I don’t see any issue with your code. The last thing I would do is check that SetVolume is called you get the value you expect from PlayerPrefs.GetFloat. Maybe for some reason, on android, PlayerPrefs.SetFloat is never called. When calling PlayerPrefs.GetFloat, if audioMixerParameterName doesn’t exist, it will return the default value, potentially causing the issue. If the value return in your start is correct, then it looks like a bug. I would then ask you to open a bug report so we can investigate. Unity QA: Building quality with passion
Did you ever figure this out? I’m stumbling into the same issue with my Android Builds. Works fine in Editor. There are several threads about this problem throughout the internet but they all end the same way: no response, no solution.
Sadly @The_Island is not working at Unity anymore. I see he asked for a bug report and I have not found that on our side. Could you submit one if you haven’t already? This is really the best way to help us address these issues.