Too kind! But thank you. Because it has been such a surprisingly common question I since wrote a whole article on this on my website here: https://johnleonardfrench.com/articles/the-right-way-to-make-a-volume-slider-in-unity-using-logarithmic-conversion/
I remember your article from a few years ago:) It helped then and im glad you reposted here - a lifesaver once again.
Must be Mathf.Log10(float f)
, because Mathf.Log(float f)
is the natural logarithm in Unity.
To 0.0001.
You are correct.
great
Thank you for the script, I was having trouble with it and also wanted to know why the calculation was like that. I read your explanation and though it didn’t make enough sense for me, thanks for this still! Have a great day everyone!
Thanks, videos are not my thing!
Sorry I’m late to the game, but I found setting the min to 0.001 to be key for my project thanks to John. Although at first, Unity wouldn’t allow me to set the value to 0.001. Every time I tried, it would reset it to 0. Because of that, it cause my music and/or SFX to go up in value rather than mute when the slider was moved all the way to the left. I just tried again, except instead of tabbing out of the min field after typing in 0.001, I clicked on the next slider and entered that value again. I rechecked both and they stayed at 0.001 instead of resetting to 0, and now the sliders mute the music and SFX as necessary. I’m not sure if anybody else has run into same problem, but in case they did, that’s what I did. Thanks again John!
Thank you!! You solved my problem
Actually, with Mathf.Log works kinda better than with Mathf.Log10. Don’t ask me why.
I’d like to add to that, Mathf.Log works just fine
Not fully correctly explanation.
It’s not related with that, But proposed solution will work (“made slider more comfortable”).
Initially slider used the decibel values, which graduated on slider at constant linear factor ((max - min) values / size of slider, in your e.g. 0 and -80 db).
When you change slider values to [0.0001; 1] you change nothing, except you begin use for SetFloat the value Mathf.Log10(sliderValue) * 20. Therefore the volume values will be in range from 0 to -80db. (if you set slider values to [0.01; 1] the range will be from 0 to -40db)
In this case you just change the scale of the slider from linear scale (values placed with one step) to a something in logarithmic scale.
It can be “Mathf.Log10(sliderValue) * 20” or “* 30” or “*19”. It’s no important.
To be honest the values on slider less than 0.01, doesn’t have any sense because can’t be set precisely values less than 0.01
The main important point is select the demanded range of volume values which you want set by a slider.
Commonly useful values in range from 0 to -40db (and have separated sliders for master volume and other volumes), in this case you can simply set slide min/max values like -40/0 and always will work the same as with “logarithmic scale”.
Note.
If you use the masterMixer.SetFloat() with “Mathf.Log10(sliderValue) * 20” don’t forget when you set initial values of slider by masterMixer.GetFloat() use the correct value for sliderValue = Mathf.Pow(10f, Volume / 20)
// Summary:
// Returns the natural (base e) logarithm of a specified number.
//
public static extern double Log(double d);
// Summary:
// Returns the base 10 logarithm of a specified number.
//
public static extern double Log10(double d);
Log(x) = Log(10) * Log10(x) ~ 2.3 * Log10(x) (difference only in multiplier)

1- Go to Assets → Create → Audio Mixer
2- Rename it as MasterMixer and double click on it
3- In the Groups section click on + and add a group. Rename it as Music
4- Select your group(Music), right click on the free space of the Attenuation in the inspector, click Expose.
5- Click on Exposed Parameters in the Audio Mixer panel. Rename parameter name as musicVol and press enter
6- Create the Game Object and name it as AudioObj. Click Add Component in the inspector and add Audio Source
7- Click on small circle in the right side of the AudioClip. Select your audio
8- Click small circle in the right side of the Output. Select MasterMixer → Master → Music
9- Go to Assets → Create → C# script
10- Add this code. (Use SetFloat name as the parameter name you gave to the Exposed Parameters)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;public class AudioScript : MonoBehaviour {
public AudioMixer masterMixer;
public void SetSound(float soundLevel)
{
masterMixer.SetFloat (“musicVol”, soundLevel);
}
}11- Create a Game Object name it as AudioScriptObj and drag the script to it
12- Drag Audio Mixer from the project panel and drop to the masterMixer
13- Go to Game Object → UI → Slider
14- Click the slider. In the inspector click the + of the On Value Changed
15- Drag the AudioScriptObj to the object and click on function list. select AudioScript → SetSound (it’s under Dynamic float)
16- Click the slider and change the Min Value to -80 and Max Value to 0
That’s it. Cheers!
Thank you!

Hi, the problem is that the fader value (-80db - 0db) is a logarithmic scale and the slider value is linear. For example: half volume is actually about -10db, but if you connect it to a linear scale, like the slider, then half volume will end up being -40db! which is why it sounds like it’s basically silent at that point.
There’s an easy way to fix it.
Instead of setting the slider min / max values to -80 and 0 (like you would to match the slider), set them to min 0.001 and max 1.
Then in the script to set the value of the exposed parameter, use this to convert the linear value to an attenuation level:
masterMixer.SetFloat("musicVol", Mathf.Log(soundLevel) * 20);
It’s important to set the min value to 0.001, otherwise dropping it all the way to zero breaks the calculation and puts the volume up again.
Hope that helps!
John.
Thank you very much!

Hi, the problem is that the fader value (-80db - 0db) is a logarithmic scale and the slider value is linear. For example: half volume is actually about -10db, but if you connect it to a linear scale, like the slider, then half volume will end up being -40db! which is why it sounds like it’s basically silent at that point.
There’s an easy way to fix it.
Instead of setting the slider min / max values to -80 and 0 (like you would to match the slider), set them to min 0.001 and max 1.
Then in the script to set the value of the exposed parameter, use this to convert the linear value to an attenuation level:
masterMixer.SetFloat("musicVol", Mathf.Log(soundLevel) * 20);
It’s important to set the min value to 0.001, otherwise dropping it all the way to zero breaks the calculation and puts the volume up again.
Hope that helps!
John.
That is great, but I found out thanks to this tutorial:
https://www.youtube.com/watch?v=pbuJUaO-wpY
that using Log10() instead of Log() yields more accurate results.
So use Log10() instead.

That is great, but I found out thanks to this tutorial:
https://www.youtube.com/watch?v=pbuJUaO-wpY
that using Log10() instead of Log() yields more accurate results.
So use Log10() instead.
Must be Mathf.Log10(float f), because Mathf.Log(float f) is the natural logarithm in Unity. To 0.0001.

You are correct.
Well, care to edit the original message so people stop copying garbage?

Actually, with Mathf.Log works kinda better than with Mathf.Log10. Don’t ask me why.
Yeah, let’s slap random functions that are slightly similar to the correct ones and then wonder why the game is full of bugs.