Slider.value doesn't show the value in real-time but after the execution of the script
Hi there, This is my first time writing a question, Basically i have a problem white the slider.value, i want that the value goes from 0 to 100. I did a simple script to calculate the factorial of big number like 120000, so i decided to put a progress bar (slider) that show at which point of the processing is the programm, so i did a simple script that divides the user numbeenter code herer by 100 so that at each iteration it adds that figure and reaches 100, but the problem is that the program does not show me how the loading bar gradually rises but only when it has finished.
here's the code:
quote = 100 / user_Number;
slider.maxValue = 100f;
slider.minValue = 0f;
slider.value = 0f;
for (int i = 1; i <= user_Number; i++)
{
for (int q = 0; q < 510001; q++)
{
//Do Stuff
}
for (int e = 0; e < 510001; e++)
{
//Do Stuff
}
progress += quote;
slider.value = progress;
}
Then if i create a Debug.Log("value: " + slider.value); in the console i can see that it works but only when the execution of the script end. Thank a lot for the help and patience Guys <3
Answer by Cuttlas-U · Apr 29, 2020 at 10:24 PM
hey; u should use a Corutine as an IEnumerator function ; this way you can put delay between your code lines ;
so each time u add a little to the progrees and u also show it on the slider;
IEnumerator My_IE_Function()
{
quote = 100 / user_Number;
slider.maxValue = 100f;
slider.minValue = 0f;
slider.value = 0f;
for (int i = 1; i <= user_Number; i++)
{
for (int q = 0; q < 510001; q++)
{
//Do Stuff
progress += quote/ 510001;
slider.value = progress;
yield return null;
}
for (int e = 0; e < 510001; e++)
{
//Do Stuff
progress += quote/ 510001;
slider.value = progress;
yield return null;
}
}
}