hi all does anyone knows how to make a slider control volume form audio source ?
i made this but it goes from 1 to 0 like it was set to be INT not float and if i fill the slider nothing happens
need your help guys
public class Volume : MonoBehaviour
{
private AudioSource Audiosource;
private float MusicVolume =1f;
void Start()
{
Audiosource = GetComponent();
}
void Update()
{
Audiosource.volume = MusicVolume;
}
public void SetVolume(float vol)
{
MusicVolume = vol;
}
}
I’m not sure why you’re setting the audiosource volume in update instead of just changing it directly in SetVolume.
But that being said, I’m not seeing any issues with what you’re showing. So you’ll need to see what value you’re getting from the slider. Debug.Log can be your friend here.
I am new to game development /coding and Unity what I’m trying to do here is simply change or control the volume of my audio source with slider via Code so if you know any syntax for that in C sharp please do share P.s i don’t know how to use setVolume thanks
I’d change the code to as below. In the inspector, drag the UI slider component over to the VolumeSlider field to set its reference. On the slider, configure it so WholeNumbers is unchecked (false) with a min value of 0 and a max value of 1. In the slider’s OnValueChanged event, add the VolumeSliderChanged method.
public class Volume : MonoBehaviour
{
private AudioSource Audiosource;
public Slider VolumeSlider;
void Start()
{
Audiosource = GetComponent<AudioSource>();
}
public void VolumeSliderChanged()
{
Audiosource.volume = VolumeSlider.value;
}
}
So what the above does, is any time the player adjusted the slider it will automatically call VolumeSliderChanged(), which will then take the value from the slider and set it as the volume of AudioSource.
https://docs.unity3d.com/Manual/script-Slider.html
Also, don’t post worthless polls. It is bad form to do something to piss off the same people you’re asking for help. Include a poll when the poll question and answer choices are relevant to the thread.
thanks alot man