- Home /
StartCoroutine to delay scene make freeze my whole scene
Im newbie in unity and i want make delay process for adding process of number. I use coroutine but when i play it, it is freezing.
Here it my code : Please help me
Note : ax, ay, and az are double variable
public void startCountLiquid(){
mainSlider.interactable = false;
ax = setV1 (getInputV2());
ay = (double)mainSlider.value / 10;
az = ax;
while((az - ay) > 0){
startPraktikum.interactable = false;
StartCoroutine (holdASecond());
}
selesai.text = "Selesai";
}
IEnumerator holdASecond(){
yield return new WaitForSeconds(3);
az-=ay;
}
Answer by Bioinformatizer · Jul 09, 2019 at 11:11 PM
It looks like you are starting a new coroutine ever time that while loops runs and it is causing an infinite loop. the holdASecond() routine never gets to complete.
change the while to an if :
`if (az - ay) > 0){
StartCoroutine (holdASecond());
startPraktikum.interactable = false;
}`
and then put selesai.text = "Selesai";
into the coroutine
But i need startCountLiquid() run until the (az - ay) become 0 or less than 0. Is it need looping or just if?
Answer by bpaynom · Jul 10, 2019 at 08:20 AM
I changed your startCountLiquid return type from void
to IEnumerator
and add a yield
inside the while bucle. If you need to call startCountLiquid from an OnClick()
event from UI, you can just add another function to call it. Something like public void StartCountDown() { StartCoroutine( startCountLiquid() ); }
public IEnumerator startCountLiquid(){
mainSlider.interactable = false;
ax = setV1 (getInputV2());
ay = (double)mainSlider.value / 10;
az = ax;
while((az - ay) > 0){
startPraktikum.interactable = false;
yield return holdASecond();
}
selesai.text = "Selesai";
}
IEnumerator holdASecond(){
yield return new WaitForSeconds(3);
az-=ay;
}