- Home /
Need help with my slider
So im finding the hardest part about coding is knowing how to ask the right questions. I spent all day trying to figure this "simple" slider out haha. Ive finally decided to come back here and ask the pros. So here goes.
I have a slider, i have music that plays, i want the max length of the slider value to be the max length of the song so i can use the slider to work my way through a song. from all i can find out, it is supposed to just be AudioClip.Length, but visual studio can't seem to find .Length(or .length) on either AudioClip, or audioClip.
Here is the code i have so far
public class musicTime : MonoBehaviour {
public AudioSource audioClip;
public Slider audioSlider;
void Start()
{
audioSlider.minValue = 0f;
audioSlider.maxValue = audioClip.length;
}
void Update()
{
}
public void timeScale()
{
}
Answer by antx · May 03, 2017 at 01:08 PM
The following works for me:
using UnityEngine;
using UnityEngine.UI;
public class AudioSlider : MonoBehaviour
{
public AudioClip audioClip;
public Slider mySlider;
void Start ()
{
mySlider.minValue = 0.0f;
mySlider.maxValue = audioClip.length;
Debug.Log("<color=orange>audio clip length: </color>" + audioClip.length);
}
}
If VS does not know AudioClip and such, perhaps you missed the usings on top?
It knows AudioClip. This code isn't actually updating the "value" on the slider though, it just stays at 0.
I should note that the song doesn't start until i hit a button.
Answer by Piyush_Pandey · May 03, 2017 at 01:31 PM
You mistakenly wrote public AudioSource audioClip;
The type is of AudioSource not AudioClip.
There is no variable AudioSource.length in AudioSource.
Use instead: public AudioClip audioClip;
How would i reference the AudioClip from my Audio$$anonymous$$anager game object, or should i need to? The song doesn't play until i hit a button. Also how would i get the "Time" value to update the slider?
U can use this: audioSource.clip
This will give you the clip that is present in your audio source {u can change that at runtime}
To get time use audioClip.timeSamples
For more info u can check this link: https://docs.unity3d.com/ScriptReference/AudioSource.html
If u want the song to play a start, just check the Play On Awake bool in the audio source component
Answer by KukadiyaPrince · May 04, 2017 at 06:46 AM
try this : public class AudioLength : MonoBehaviour {
#region Public_Variables
public AudioClip audioClipLength; //Attach AudioClip
public Slider slider;//Attach Slider
#endregion
void Start()
{
float length;
length = audioClipLength.length;
Debug.Log("length" + length);
slider.GetComponent<Slider>().maxValue = length;
}
}
Your answer
Follow this Question
Related Questions
Music play throughout all scenes? 1 Answer
Multiple, independent volume sliders 1 Answer
How to make volume continually go down in C#Script ? 1 Answer
Controlling Master Volume with one slider? 1 Answer
Multiple Cars not working 1 Answer