Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Bartowski89 · May 03, 2017 at 11:41 AM · c#slidermusicvolume

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()
     {
         
 
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bartowski89 · May 03, 2017 at 03:03 PM 0
Share

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.

avatar image
1

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;

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bartowski89 · May 03, 2017 at 03:18 PM 0
Share

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?

avatar image Piyush_Pandey Bartowski89 · May 05, 2017 at 07:42 AM 0
Share

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

avatar image Piyush_Pandey Bartowski89 · May 05, 2017 at 07:43 AM 0
Share

If u want the song to play a start, just check the Play On Awake bool in the audio source component

avatar image
0

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;
     }
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges