Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Stealthygolem · Oct 27, 2014 at 01:41 PM · c#ifstatewhile

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
     }

Comment
Add comment · Show 11
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 Unitraxx · Oct 27, 2014 at 01:57 PM 0
Share

Can you post the code that caused your unwanted behavior?

avatar image MrSoad · Oct 27, 2014 at 01:57 PM 0
Share

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.

avatar image perchik · Oct 27, 2014 at 02:03 PM 0
Share

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

avatar image MrSoad · Oct 27, 2014 at 02:05 PM 0
Share

perchik is also giving you very good advice there! :D

avatar image _Yash_ · Oct 27, 2014 at 02:08 PM 0
Share

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.

Show more comments

1 Reply

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

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.

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 Stealthygolem · Oct 27, 2014 at 02:55 PM 0
Share

Oh god, I am so emabarrassed... haha. Thank you! I will go take a break now. It worked, btw. ^^

avatar image Unitraxx · Oct 27, 2014 at 03:00 PM 0
Share

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. :)

avatar image Stealthygolem · Oct 27, 2014 at 03:28 PM 0
Share

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

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

32 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 avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Will C# in Unity validate/check the state of a boolean every frame or will it just remember the state and change its state when needed? 1 Answer

Does This Make Sense? Is It Correct? 2 Answers

Save scene state after reloading[solved] 0 Answers


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