- Home /
C# Check for boolean value with while loop in Update function
Hello! I am working on adding boss-states. I am working off my prior knowledge and creating states out of logical if statements and boolean variables. So, what I got working right now, is that it does pick a random new number, then sets the boolean in range of the number given to true, and sets both of them false when my timer is at 1 (1 second before I assign a new random number).
The problem, is that when I try to collect the method for "BossProjectileAttack()" or "BossSpiralAttack()" it just always remains true, and/or bugs out.
Relevant code:
public bool spiralAttack = false;
public bool projectAttack = false;
void Update () {
if(attackStateChange > 0) {
attackStateChange -= Time.deltaTime;
}
if(attackStateChange < 0) {
attackStateChange = 0;
}
if(attackStateChange == 0) {
attackStateChange = attackStateTimer;
float rand = Random.RandomRange(1,10);
if(rand < 5) {
spiralAttack = true;
}else {spiralAttack = false;}
if(rand >= 5) {
projectAttack = true;
}else {projectAttack = false;}
}
else if(attackStateChange == 1) {
spiralAttack = false;
projectAttack = false;
}
/*HELP IS NEEDED HERE
if spiralAttack = true, then BossSpiralAttack, OR
while spiralAttack = true, then BossSpiralAttack
?*/
void BossProjectileBasic(){
//blablabla
}
void BossSpiralAttack(){
//blablabla
}
Can you post the code that caused your unwanted behavior?
The problem might be to do with you checking "attackStateChange" against an integer(whole number). You are changing its value by Time.deltaTime at one point which is a float value. Comparing a float to an int can cause unwanted results. Always declare a var type explicitly and always do compares for the type properly, 0.0 not 0, or 0f not 0.
Also look at "enums" which are collections for your states in conjunction with "switch" type statements rather than "if" statements for better less bug prone code.
You may also need to change your if statements to else if statements. Right now, it might fall into the first case, get changed, and then fall into the next case
what can i see is it will never go inside if(attackStateChange == 1) {...} because if you set attackStateChange to 1 inside if(attackStateChange == 0) {...} it will not execute "else if" and in next frame update will decreament it by time.deltatime.
Answer by Unitraxx · Oct 27, 2014 at 02:50 PM
Exactly what I thought:
if(spiralAttack = true) {
BossAttackSpiral();
}
Again the classical mistake of using the assignment operator =
instead of the equality operator ==
. Use instead
if(spiralAttack == true) {
BossAttackSpiral();
}
or simply
if(spiralAttack) {
BossAttackSpiral();
}
to avoid confusion.
Oh god, I am so emabarrassed... haha. Thank you! I will go take a break now. It worked, btw. ^^
You're welcome, but in the future when you ask another question be sure to include the code that actually gives you the unwanted behavior as well. :)
I'll try to remember. The reason I didn't add one, is that I tried several different variants. So i felt it would be more cleaner to leave the space open, and just state "what I want to code here is:". Thank you again :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613170049im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Does This Make Sense? Is It Correct? 2 Answers
Save scene state after reloading[solved] 0 Answers