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 mightybob · Nov 08, 2014 at 03:04 AM · javascriptifsyntax

if ((this || that) && (that || this)).. Does that work?

Hey, I was trying to write some code to show the scoreboard when player 1 and 2 either don't finish or finish. So far I got this:

 if ((player1nf || player1Finished) && (player2nf || player2))
 {
     showScoreBoard();
 }

Although it didn't give me any errors, I have a feeling the syntax of the if() isn't correct. I was wondering how I would use ||'s along with &&'s to create something like I have in the title, if that makes any sense.

Thanks a ton.

Comment
Add comment · Show 2
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 kburkhart84 · Nov 08, 2014 at 04:32 AM 0
Share

I'm not going to post an answer as a perfectly valid one is already there.

But, assu$$anonymous$$g you have the right variable name(player2, which is not the same as the ones in player1's vars), and that you indeed are setting those variables correctly, than the statement is valid and should work.

The reason it should work like you think is because of your parenthesis. It separates two sides of an AND logic gate, and you happen to have two separate OR logic gates, one on each side of the AND. You could have something else, like one single bool and the other an OR, or basically whatever.

$$anonymous$$y question here could be simply whether this is the best way to do what you want. It is pretty simple so could work fine, but for example, you maybe could ins$$anonymous$$d of this, have a single variable for the "state" of a player, where 0 could be "playing" 1 could be "done/lost" and 2 could be "done/won" where lose and win is deter$$anonymous$$ed by whether they "finished." In fact, you could use an enum(look it up) to control these variables.

avatar image Owen-Reynolds · Nov 08, 2014 at 05:32 AM 1
Share

This is just a general program$$anonymous$$g question. Ifs, Ands and Ors in Unity work the same as anywhere else.

You should be able to look-up and get more better detail on If tricks from general program$$anonymous$$g web sites.

2 Replies

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

Answer by Habitablaba · Nov 08, 2014 at 03:06 AM

(My answer got eaten?)

That is a totally valid if statement, but there is really no way to know if it does what you want it to do with the information given.

Logical operators follow the "inside-to-out" order of operations, just like arithmetic operators.

Consider the following:

 if((a||b) && (c||d))

a||b will evaluate first. True, if either a or b is true.
We will call this A for now. c||d will evaluate next. True, if either c or d is true.
We will call this C for now.
This leaves us with

 if(A && C)

which will evaluate next. True only if both A and C are true.
This means for your specific situation, that each grouping needs to have at least one true in it to drop into the if block.

Does that clear it up a bit?

Comment
Add comment · Show 4 · 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 Bunny83 · Nov 08, 2014 at 05:55 AM 0
Share

You have a small error in your sequence ;)

  • a || b will evaluate first, let's call this A

  • If A evaluated to false we're done here and the if block will not be executed. c and d aren't evaluated at all

  • If A evaluates to true it will continue to evaluate the right-hand operand of our && operator which is also a bracket term

  • If what's inside the brackets evaluates also to true the if is executed.

What you have explained would happen with the "&" operator. The conditional "and" operator && quits as soon as the outcome is clear.

Same for the conditional "or" operator || , however there it's of course reversed. If the left-hand operand is true it won't even evaluate the right-hand operand. However if the left hand operand is false it will look at the right-hand operand as well.

For plain variables that's actually quite irrelevant. However if you have a method call inside the bracket it will be important. For example:

 bool someVar = true;
 if (someVar || Physics.Raycast(ray, out hit))
 {
     // ...
 }

Here it won't call Physics.Raycast at all because someVar is true. That means inside the if-body hit is not being set if someVar is true.

You might want to have a look at the "Operator precedence and associativity" page which also shows the order of the operators. In our example here the brackets are important because the && operator is evaluated before ||. That means

 if(a || b && c || d)

is acutally

 if(a || (b && c) || d)


avatar image Redeemer86 · Nov 08, 2014 at 06:03 AM 0
Share

The above logic is a bullseye .. But just to add you must check if the logical context you desire is actually in line with the above logic. I am a bit confused to the nature of the variables and their settings for your purpose... You must also ask yourself if the variables you used in the if statement share a logical relation. You must be very careful if you are using interdependent variables...

Example.

Player1finished =!player1nf

Red

avatar image Habitablaba · Nov 08, 2014 at 06:23 AM 0
Share

@Bunny83 I intentionally left out short cutting because I figured if they are asking this type of question, then the very basics will do fine. You are, of course, correct though.
+1 for the link to operator precedence, definitely something that should be kept in $$anonymous$$d, especially as you start to learn 'clever' program$$anonymous$$g tricks.

avatar image mightybob · Nov 10, 2014 at 10:13 PM 0
Share

Thanks this clears it up, my code was fine but if one isn't true it won't continue.

avatar image
0

Answer by Redeemer86 · Nov 08, 2014 at 08:10 AM

simply use ...

if(player1finished || player2finished)

Red

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 mightybob · Nov 10, 2014 at 10:14 PM 0
Share

That wasn't my question, and isn't usable for what I'm doing since I want to detect if the players have not finished or died also.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

How are the if's in a GUI.Toolbar handled? 1 Answer

How to Less Than or Equal to with a Raycast? 1 Answer

Multiplayer Support for Android 1 Answer

Targetting script bug. (Javascript) 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