- Home /
 
Do-While loop
Hello i have small problem with my do-while loop. I dont know what else i can use here
first of all i need this for my progress bar.
My script work fine until aeg = 1 and then freeze. But when i use while(aeg <= 1); then it start infinity loop starting with 1 and going forever.
 if(Calc_Exp >= vaja_Exp){
                 vaja_Exp = (vaja_Exp*2);
                 level += 1;
                 Level.text =("" + level);
                 Calc_Exp -= vaja_Exp;
                 do{
                     aeg += Time.deltaTime;
                     Debug.Log(aeg);
                     Bar_Exp.transform.localScale = new Vector3(aeg, Bar_Exp.transform.localScale.y, Bar_Exp.transform.localScale.z);
                 }while(aeg >= 1);
 
               EDIT: and it doing also outside do-while loop
when i put
 do{
                      aeg += Time.deltaTime;
                      Debug.Log(aeg);
                      Bar_Exp.transform.localScale = new Vector3(aeg, Bar_Exp.transform.localScale.y, Bar_Exp.transform.localScale.z);
                  }while(aeg >= 1);
 aeg = 0;
 
               this always put aeg back to 0;
i try making filling status bar that what fill with time.
to be more specific
i make game where u get exp and when u die then pop up death menu and score will be converted to exp and with time exp bar will fill
Solution
I needed change good very hard :D i did this with if statements here is my finnal working code, this work way it supposted to work.
     void Update () {
         if (activebool == true) {
             skoor = SchoreManager.scoreshare;
             //Cur_Exp += skoor / 100;
             aeg = Calc_Exp;
             activebool = false;
         }
 
         if (Cur_Exp >= vaja_Exp) {
             if(aeg < 1){
             aeg += Time.deltaTime;
             Bar_Exp.transform.localScale = new Vector3(aeg, Bar_Exp.transform.localScale.y, Bar_Exp.transform.localScale.z);
             }
             if(aeg >= 1){
                 aeg = 0;
                 Cur_Exp -= vaja_Exp;
                 level += 1;
                 Level.text =("" + level);
                 vaja_Exp = (vaja_Exp * 2);
             }
         }
         if (Cur_Exp < vaja_Exp) {
             Calc_Exp = Cur_Exp/vaja_Exp;
             if(aeg < Calc_Exp){
                 aeg += Time.deltaTime;
                 float jooks = aeg;
                 Bar_Exp.transform.localScale = new Vector3(jooks, Bar_Exp.transform.localScale.y, Bar_Exp.transform.localScale.z);}
         }
     }
 
               But thanks for reply for those who did it.
Answer by Bafelmauk · Nov 27, 2015 at 07:05 PM
for those who have same problem
     void Update () {
         if (activebool == true) {
             skoor = SchoreManager.scoreshare;
             //Cur_Exp += skoor / 100;
             aeg = Calc_Exp;
             activebool = false;
         }
 
         if (Cur_Exp >= vaja_Exp) {
             if(aeg < 1){
             aeg += Time.deltaTime;
             Bar_Exp.transform.localScale = new Vector3(aeg, Bar_Exp.transform.localScale.y, Bar_Exp.transform.localScale.z);
             }
             if(aeg >= 1){
                 aeg = 0;
                 Cur_Exp -= vaja_Exp;
                 level += 1;
                 Level.text =("" + level);
                 vaja_Exp = (vaja_Exp * 2);
             }
         }
         if (Cur_Exp < vaja_Exp) {
             Calc_Exp = Cur_Exp/vaja_Exp;
             if(aeg < Calc_Exp){
                 aeg += Time.deltaTime;
                 float jooks = aeg;
                 Bar_Exp.transform.localScale = new Vector3(jooks, Bar_Exp.transform.localScale.y, Bar_Exp.transform.localScale.z);}
         }
     }
 
              Answer by tanoshimi · Nov 26, 2015 at 07:30 PM
Did you mean:
 aeg -= Time.deltaTime;
 
               ? Otherwise, this is an infinite loop - aeg gets bigger and bigger forever.
i think it should be yea but when i but
 while(aeg <= 1);
 
                  then it will going forever and when
 while(aeg >= 1);
 
                  then it will freeze and i think i should use
  aeg += Time.deltaTime;
 
                  because the valume moust increase 0 to 1 when reach 1 then it should be go back to 0
Well, do you want aeg to get bigger or smaller? You can't just arbitrarily swap! And, if you want this to take place over several frames you need this to be in a coroutine with a yield in the while loop anyway - I hope this isn't in Update()?
it is under update :D should i put it under start() ? when i puted this under start() and i needed put, because otherwise it was max with very fast not with 1 sec
 aeg += Time.deltaTime/10;
 
                   with this code it freezed abit and then hopped to 1
and yea aeg should go bigger
here is my full code:
 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class ExpBar : $$anonymous$$onoBehaviour {
     public float $$anonymous$$ax_Exp = 0;
     public float Cur_Exp = 0;
     public float vaja_Exp = 10;
     public GameObject Bar_Exp;
     public Text Level;
     public int level = 0;
     public float skoor;
     public float aeg;
     // Use this for initialization
     void Start () {
         float Calc_Exp;
         Level.text =("" + level);
         skoor = Schore$$anonymous$$anager.scoreshare;
         if (skoor >= 100){
             Calc_Exp = (skoor/100);
             /*if ( Calc_Exp < vaja_Exp){
                 Cur_Exp = (Calc_Exp / vaja_Exp);
                 skoor -= vaja_Exp;
                 Bar_Exp.transform.localScale = new Vector3(Calc_Exp, Bar_Exp.transform.localScale.y, Bar_Exp.transform.localScale.z);
                 }*/
             if(Calc_Exp >= vaja_Exp){
                 vaja_Exp = (vaja_Exp*2);
                 level += 1;
                 Level.text =("" + level);
                 Debug.Log(Calc_Exp);
                 Calc_Exp -= vaja_Exp;
                 Debug.Log(Calc_Exp);
                 do{
                     aeg += Time.deltaTime/10;
                     Debug.Log(aeg);
                     Bar_Exp.transform.localScale = new Vector3(aeg, Bar_Exp.transform.localScale.y, Bar_Exp.transform.localScale.z);
                 }while(aeg <= 1);
             }
             else{
                 
             }
         }
 
     }
     
     // Update is called once per frame
     void Update () {
     }
 }
 
                  with this code
     void Start () {
         do{
             aeg += Time.deltaTime;
             Debug.Log(aeg);
             Bar_Exp.transform.localScale = new Vector3 (aeg, Bar_Exp.transform.localScale.y, Bar_Exp.transform.localScale.z);
         }while(aeg <= 100);
     }
 
                    this not move as motion this freez and run again when aeg = 100
Your answer
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Callback from a Loop 1 Answer
How to make the loop work 2 Answers