Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Slider maxValue Question.

Discussion in 'Scripting' started by slykrooper, Apr 18, 2021.

  1. slykrooper

    slykrooper

    Joined:
    Oct 27, 2018
    Posts:
    10
    Hey guys, i've got a question about the maxValue on the UI slider component. I want to change the sliders max value to equal the length of an audioclip. I made it so the slider maxValue is equal to the audiosource.clip.length, and no errors there! However in the inspector it still has its values to 1 for the max. I want to make a bunch of these off of other audioclip lengths so im wondering if theres a way to disable the maxValue in the inspector or something like that. Thank you so much!

    Heres the code for it if you need it:

    Code (CSharp):
    1. {
    2.     public AudioSource audioSource;
    3.     public Slider audioSlider;
    4.  
    5.     // Start is called before the first frame update
    6.     void Start()
    7.     {
    8.         audioSlider.direction = Slider.Direction.LeftToRight;
    9.         audioSlider.minValue = 0;
    10.         audioSlider.maxValue = audioSource.clip.length;
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.         audioSlider.value = audioSource.time;
    17.     }
    18. }
     
  2. I just put it together your code with a quick test rig and everything is working after entering play mode as it should be.

    screenshot1.png
     
    diXime likes this.
  3. slykrooper

    slykrooper

    Joined:
    Oct 27, 2018
    Posts:
    10
    Weird. Maybe I messed up a setting somewhere. Thank you for testing that for me! I appreciate your time!