Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Bafelmauk · Nov 26, 2015 at 06:03 PM · c#loopwhile-loopwhile loop

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);}
         }
     }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bafelmauk · Nov 26, 2015 at 07:49 PM 0
Share

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

avatar image tanoshimi · Nov 26, 2015 at 07:56 PM 0
Share

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()?

avatar image Bafelmauk tanoshimi · Nov 26, 2015 at 08:12 PM 0
Share

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 () {
     }
 }
 
avatar image Bafelmauk Bafelmauk · Nov 26, 2015 at 08:25 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

31 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Initialising List array for use in a custom Editor 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges