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 anaz97 · Dec 17, 2015 at 10:35 AM · if statementwhile

Why this crashes unity ? [Do While & If]

i writed this script for testin difference between do while and if statement so why when i hit play the DO float become 2 even if i have the "operate" bool false and why when i hit play my editor stucks, keeps 50% of my processor usage, and doesn't let me see the IF float augmenting?

 using UnityEngine;
 using System.Collections;
 
 public class lala : MonoBehaviour {
 
     public float DO;
     public float IF;
     public bool operate = false;
     private float DObase = 1f;
     private float IFbase = 1f;
 
     void Update () {
         do{
             DO = DObase + 1f;
         }while(operate == true);
      if (operate == true) {
             IF = IFbase + 1f;
         }
     }
 }
 
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
1
Best Answer

Answer by Soraphis · Dec 17, 2015 at 08:40 PM

update is called once per frame, unity is using something called an update loop. You can imagine somthing like: calling the Update() method from each script and then rendering an image.

if one would write an update() method which takes unusually long, it would slow down the program because it takes longer till unity can render the next image. now imagine someone would write an update method which never ends. unity could never reach the point to render the next frame -> the editor freezes.

 void Update () {
     do{
         DO = DObase + 1f;
     }while(operate == true);
     if (operate == true) {
         IF = IFbase + 1f;
     }
 }


a do{ ... }while(); loop is the same as a while(){ ... } loop, with the difference it will always run at least once. Your loop will be execute till operate gets false, so imagine operate is true when you enter this update loop. operate is never changed in your loop, there are no breaks, so you will never reach a state where your condition in the while-head gets false, so you build an infitiy-loop.

So, lets check what your loop does:

your loop uses the value of DObase (which you've set to 1) adds 1 to it, and puts that in the variable DO. so your variable DO is now 2. because DObase does not change, DOBase+1 will always be 2.

also your CPU will get stuck in your do-while loop, so you'll never reach the line where you want to increment IF


You could try to tell us (maybe in another question) what you want to archive if you need help. Becaues i have no clue what you are trying to do anyway. But i hope this explains what went wrong

Comment
Add comment · Show 3 · 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 anaz97 · Dec 17, 2015 at 09:50 PM 0
Share

yes I realized it later that for keep augmenting the IF float i have to add 1 to itself for every frame, and then: IF = IF + 1 , that was a $$anonymous$$e stupid error, also thank to you i understood why it doesn't even reach the line where i made IF = IFbase +1 (1+1=2) andlet me see at least the result of the addition,

but, from what i understood, do-while function like that: do{a thing}while(condition) so like a if statement that is executed once

and for that i don't understad why it execute the addiction do{ DO = DObase + 1f; }while(operate == true);

while the the condition, operate is false

i'm actually doing that because i'm a "tweaker"(?) i've not ever sudied program$$anonymous$$g at school or stuff like that, and i can't for the next year and a half, a year ago i started doing it myself, so i have to try and experiment anything in order to understand it

avatar image Soraphis anaz97 · Dec 17, 2015 at 09:57 PM 0
Share

I'd recommend to you searching an tutorial for general (object oriented) program$$anonymous$$g.

a do-while-loop is a loop. as long as the condition in the while-head is true, the code in the do{...} body is executed. if you want an if statement, just use an if statement.

a do-while loop will always execute the body once, because it reaches the condition after executing the body.

a while loop checks the condition first, and will execute the body just while the condition is true

and i have no clue what a 'tweaker' is (im not a native english speaker myself)

avatar image anaz97 Soraphis · Dec 18, 2015 at 12:51 PM 0
Share

O$$anonymous$$ i'll go for it

I thank you fo the reponse!

avatar image
1

Answer by allenallenallen · Dec 17, 2015 at 10:38 AM

So you have 2 questions. The first one is why DO becomes 2. The second one is why your editor freezes.

The answer to your first question is because you wrote it that way. Do - While will run what's under Do once and then check if the While conditions are met. So it's perfectly normal for DO to become 2f when you hit play regardless of what "operate" is.

The answer to your second question is I'm guessing that you manually checked "operate" to be true in the editor. So when the script runs, it goes into an infinite loop trying to add IF and DO to infinity.

Comment
Add comment · Show 1 · 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 anaz97 · Dec 17, 2015 at 08:05 PM 0
Share

-first question: until now i don't understand the do-while thing... not even reading the documentation, maybe because i've not studied those things

-second question: i realized that is this do{ DO = DObase + 1f; }while(operate == true); that freezes the Editor

-third question: was a stupid thing IF = (IFbase = 1) + 1 = 2 and can't augment, for making it augmenting i have to write IF = IF + 1

anyway thank you for the reponse

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

TBG,If,While 1 Answer

While Button Pressed 3 Answers

make Update display/animate iterations of While(i<20)loop? 2 Answers

while loop not looping 2 Answers

IEnumerator while loop for charge weapon error. 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