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 OctoMan · Apr 15, 2015 at 01:12 PM · booleanmovingcompareoperator

How to handle different bools in 1 statement

In different trigger i set variables to true or false. If the trigger is not hitting any, the unit should move.

In the update i want to test if those variables are false and move if so.

 if(!in_range)
         {
             move();
         }

works so far. But.

 if(!in_range || !green_in_range)
             {
                 move();
             }

does not.

 void move()
     {
         current_speed = speed;
         transform.Translate (Vector3.right * current_speed);
     }

The unit keeps moving. I wonder why. Am i using the wrong Operator? Thanks in advance.

Comment
Add comment · Show 1
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 Landern · Apr 15, 2015 at 01:37 PM 0
Share

Something isn't jiving.

Using a logical OR (||) is an early bail on a chain of evaluations of true in the condition expression. If !in_range evaluated to be true, then the second part(or the rest) of the logical OR in the expression would be skipped since you only need one in the series to be true. So this is the case of an either. If you want BOTH to be evaluated for true, you would use a logical AND (&&).

1 Reply

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

Answer by YoungDeveloper · Apr 15, 2015 at 01:14 PM

We can't see how you set up your booleans. Are your trying to check OR (||) or AND (&&)? Try this with debugs.

 if(!in_range){
     Debug.Log("in_range");
     if(!green_in_range){
         Debug.Log("green_in_range");
         move();
     }
 }


Comment
Add comment · Show 10 · 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 OctoMan · Apr 15, 2015 at 01:25 PM 0
Share

I set them in the triggers.

 void OnTriggerStay(Collider other)
     {
         if(other.tag == "red")
         {
             in_range = true;
         }
 
         if (other.tag == "green_feeler") 
         {
             green_in_range = true;
         }
 
 void OnTriggerExit(Collider other)
     {
         in_range = false;
         green_in_range = false;
     }

and the set correct as i can see in the inspector.

I want to check if any of those bools is false. So in_range OR green_in_range. So the unit can move.

Your code piece excludes green_in_range when in_range is true, isn't it?

avatar image YoungDeveloper · Apr 15, 2015 at 01:39 PM 0
Share

In my example, if in_range is false it will only go what's inside the brackets. Then it checks green_in_range, if it's false it will only then go and execute the debug on move. So at the end move will be executed only if both are false. Your Ontrigger is incorrect, you are setting both false even if only one exits it.

 void OnTriggerEnter(Collider other){
          if(other.tag == "red"){
              in_range = true;
          }
  
          if (other.tag == "green_feeler"){
              green_in_range = true;
          }
  
  void OnTriggerExit(Collider other){
          if(other.tag == "red"){
              in_range = false;
          }
  
          if (other.tag == "green_feeler"){
              green_in_range = false;
          }
 }




avatar image OctoMan · Apr 15, 2015 at 01:51 PM 0
Share

The trigger exit seems to fix the problem, but still not with the operator (||). Only with your first piece of code. Any idea why? how would i handle 3 booleans? The same way?

avatar image YoungDeveloper · Apr 15, 2015 at 01:55 PM 0
Share

In my example i use AND, because both must be false to trigger move(). In your example either one or other must be false to run move(). You still didn't said you need both of them false or just one.

Have you tried this?

 if(!in_range && !green_in_range){
     move();
 }
avatar image OctoMan · Apr 15, 2015 at 02:05 PM 0
Share

I need both to check if they are false independent from the other. If in_range is true they should do attacking stuff. But not moving. If green_in_range is true they just wait for the unit in front to move or die. In_range is triggered when an enemy is in front. Green_in_range is triggered if a friendly unit is in front.

However i may need some more states to check like, if a build or base is in range. Thats why i asked if its possible to use more bools in your way.

Hope this makes sense. AND(&&) is not what i need. I need OR(||) (if any of this states become true - dont move)

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Script throwing error Operator '==' cannot be used with a left hand side of type System.Object and right hand side of type Controller 1 Answer

Rotate camera around moving object 2 Answers

C# please explain this line of code 2 Answers

In Javascript, can I reverse a boolean as so: boolean = !boolean 1 Answer

How do I compare boolean variables? 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