Search Unity

hi all does anyone knows how to make a slider control music form audio source ?

Discussion in 'Scripting' started by ibrahemelraf3ee, Nov 15, 2019.

?

hi all does anyone knows how to make a slider control music form audio source ?

  1. code

    0 vote(s)
    0.0%
  2. code

    0 vote(s)
    0.0%
  1. ibrahemelraf3ee

    ibrahemelraf3ee

    Joined:
    Sep 7, 2019
    Posts:
    14
    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<AudioSource>();
    }
    void Update()
    {
    Audiosource.volume = MusicVolume;
    }
    public void SetVolume(float vol)
    {
    MusicVolume = vol;
    }
    }
     
    Last edited: Nov 15, 2019
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    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.
     
    ibrahemelraf3ee likes this.
  3. ibrahemelraf3ee

    ibrahemelraf3ee

    Joined:
    Sep 7, 2019
    Posts:
    14
    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
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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.

    Code (csharp):
    1. public class Volume : MonoBehaviour
    2. {
    3.     private AudioSource Audiosource;
    4.     public Slider VolumeSlider;
    5.  
    6.     void Start()
    7.     {
    8.         Audiosource = GetComponent<AudioSource>();
    9.     }
    10.  
    11.     public void VolumeSliderChanged()
    12.     {
    13.         Audiosource.volume = VolumeSlider.value;
    14.     }
    15.  
    16. }
    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.
     
    ibrahemelraf3ee likes this.
  5. ibrahemelraf3ee

    ibrahemelraf3ee

    Joined:
    Sep 7, 2019
    Posts:
    14
    thanks alot man