- Home /
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;
}
}
}
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
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
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)
O$$anonymous$$ i'll go for it
I thank you fo the reponse!
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.
-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
Follow this Question
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