Search Unity

Question Fill slider value on click

Discussion in 'Scripting' started by micomalatag, Apr 22, 2021.

  1. micomalatag

    micomalatag

    Joined:
    Apr 22, 2021
    Posts:
    2
    Hello! I am wondering how do I do this: on click of a button the value of the slider will fill up in a constant rate.

    Here's what I got so far,
    Code (CSharp):
    1. {
    2.         private void Start()
    3.     {
    4.         //reset all bars
    5.         progressBar.value = 0;
    6.         cooldownBar.value = 0;
    7.     }
    8.  
    9.     private void Update()
    10.     {
    11.         //constant decline of cooldown
    12.         cooldownBar.value -= data.cdRate;
    13.  
    14.         //checks if progress bar is filled up and cooldown bar has the capacity to do so
    15.         if (progressBar.value == progressBar.maxValue
    16.             && cooldownBar.value < cooldownBar.maxValue-5)
    17.         {
    18.             //resets progress bar and adds time to cooldown
    19.             progressBar.value = 0;
    20.             cooldownBar.value += 5;
    21.         }
    22.     }
    23.  
    24.     //on click function
    25.     public void fillBar()
    26.     {
    27.         //supposedly fills up the bar at constant rate
    28.         progressBar.value += 0.01f;
    29.     }
    30. }
     
  2. Okay, first I will tell you in English words what you need to do, please try to research it and attempt to do it. If you stuck somewhere, come back ask questions. I'm doing this because you learn by trying and doing and researching more than you would if I give you the straight code answer.

    So, you have the on click function which hopefully runs when you click on something, right? Let me know if I misunderstood you.
    In that method, you will need to start a new coroutine. Coroutines are like regular methods, but they can temporarily return so they can continue their execution where they left off after an allotted time, If you choose you can run a coroutine on a way it makes some progress in every frame, more or less steadily.
    So, please take the time and research coroutines.

    In that new method (coroutine), you will need a loop, probably while-loop, because you want to fill up a bar through numeric value.
    Inside that loop you will increase the value of your progressbar just as you are doing right now in your current method, except, if you want a steady rate, you will need to count for the discrepancies in framerate (different amount of time pass by in different circumstances). So you want to have your increment (what you have right now) and you want to multiply it with deltaTime (please research Time.deltaTime). Obviously, if you find that the bar fills up too slow, you can raise your increment value.

    And then inside of your while loop you need to return from your coroutine on a way Unity knows that it should continue the method next frame or after some time. You can find the answer when you research the StartCoroutine.

    Please, try to put it together and let's see how it goes and share the results or ask your questions.
     
  3. micomalatag

    micomalatag

    Joined:
    Apr 22, 2021
    Posts:
    2
    Thank you for your reply Lurking_Ninja,

    This is what I got now:
    Code (CSharp):
    1.     public void fillBar()
    2.     {
    3.         while(progressBar.value != progressBar.maxValue)
    4.         {
    5.             progressBar.value += 0.01f * Time.deltaTime;
    6.         }
    7.     }
    The progress bar somehow works as for some time after clicking it adds 5 to the cooldown bar. However, it freezes until the progress bar fills up completely. I also don't see the progress bar filling up and the cooldown bar freezes at its current value while the progress bar "fills" up.

    Edit: I added
    Code (CSharp):
    1. print("filling...");
    and after clicking, some seconds later it prints multiple 'filling...' all at once.
     
  4. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    You can use this just replace to your variables i am using this as loading bar when Steam is online.
    Code (CSharp):
    1. using TMPro;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. namespace ProjectDot
    5. {
    6.  
    7.  
    8.     public class LoginManager : MonoBehaviour
    9.     {
    10.         public Slider bar;
    11.         private RuntimeManager rm;
    12.         private float loadingSpeed = 150;
    13.    
    14.  
    15.         public GameObject SteamStatus;
    16.  
    17.         private void Start()
    18.         {
    19.           rm = FindObjectOfType<RuntimeManager>();
    20.      
    21.          
    22.             bar.value = 0;
    23.             bar.maxValue = 100;
    24.          
    25.         }
    26.  
    27.  
    28.         private void Update()
    29.         {
    30.    
    31.             if (SteamManager.Initialized)
    32.             {
    33.                 SteamStatus.GetComponent<TextMeshProUGUI>().text = "";
    34.  
    35.                 if (bar.value < bar.maxValue)
    36.                 {
    37.                     bar.value += loadingSpeed * Time.deltaTime;
    38.  
    39.                 }
    40.                 else
    41.                 {
    42.                  
    43.                     rm.CheckIfExistsSteam();
    44.                 }
    45.             }
    46.             else
    47.             {
    48.                 SteamStatus.GetComponent<TextMeshProUGUI>().text = "Steam is offline";
    49.                 SteamStatus.GetComponent<TextMeshProUGUI>().color = Color.red;
    50.             }
    51.         }
    52.     }
    53. }