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 artician · Jun 16, 2010 at 01:48 AM · operator

Unity/Javascript Operators

I am trying to use an 'or' operator in an if statement, and Unity is telling me it's an unexpected token.

What does Unity recognize as an 'or' operator? Found little in the script ref.

Thank you for the help!

Comment
Add comment
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

2 Replies

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

Answer by Jessy · Jun 16, 2010 at 02:06 AM

You use two vertical "pipes" for or.

||

http://www.w3schools.com/JS/js_comparisons.asp

It's nice, because it "short circuits". That is, if the first statement is true, it won't bother to evaluate any more statements. So if you have a statement that is likely to be true most of the time, more so than another statement, put that first for more efficient code.

Comment
Add comment · Show 2 · 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 artician · Jun 16, 2010 at 02:18 AM 0
Share

Ah, so I did have it right. What did I do wrong then?


if(Input.GetAxis("P1_HorizontalAim")) || (Input.GetAxis("P1_VerticalAim")) { isTurning = true; }


avatar image Mike 3 · Jun 16, 2010 at 02:50 PM 0
Share

if(Input.GetAxis("P1_HorizontalAim") || Input.GetAxis("P1_VerticalAim")) { isTurning = true; }

avatar image
0

Answer by Cyclops · Jun 16, 2010 at 02:28 AM

What you currently have is:

if (Input.GetAxis("P1_HorizontalAim")) || (Input.GetAxis("P1_VerticalAim")) {
    isTurning = true;
}

First, the entire conditional part of the if state needs to be in parentheses. Second, I suspect that Javascript wants a boolean value in the if - and Input is returning a float. Try this instead:

if ((Mathf.Abs(Input.GetAxis("P1_HorizontalAim")) > 0.1) || 
    (Mathf.Abs(Input.GetAxis("P1_VerticalAim")) > 0.1)) {
    isTurning = true;
}

Update: confirmed that, yet again, Eric5h5 is right - this code does work, I tested it:

function Update () {
    if (Input.GetAxis("Horizontal") || 
        Input.GetAxis("Vertical")) {
        isTurning = true;
    }
}

The only question I would have, is if physical joysticks can generate spurious values, or whether Unity filters them out below a certain threshold.

Comment
Add comment · Show 7 · 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 Eric5h5 · Jun 16, 2010 at 04:07 AM 1
Share

Actually, the float from Input.GetAxis will implicitly evaluate as a boolean, where 0.0 == false and anything else == true. So artician's original code will work fine, once the relevant bits are put in parentheses properly.

avatar image Tetrad · Jun 16, 2010 at 04:09 AM 0
Share

A lot of weird things implicitly evaluate to booleans in javascript. http://stackoverflow.com/questions/1995113/strangest-language-feature/1998224#1998224

avatar image Eric5h5 · Jun 16, 2010 at 04:27 AM 0
Share

@Tetrad: Javascript isn't Unityscript, however, so most of that table is either a syntax error or doesn't evaluate the same in Unity.

avatar image qJake · Jun 16, 2010 at 07:38 AM 0
Share

UnityScript is based on .NET/$$anonymous$$ono, and .NET does NOT implicitly convert other types to bool. So things like 0, "", and null are NOT bools. You need to explicitly check for these using logical binary operators.

avatar image Cyclops · Jun 16, 2010 at 02:51 PM 0
Share

Sigh... @SpikeX, yes, @Eric5h5 is correct - does that ever get old, @Eric5h5? :)

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

No one has followed this question yet.

Related Questions

Where did I go wrong (logical operator or tagging) 1 Answer

What is the difference between these ? 1 Answer

how do i use 'And' and 'Or' in UnityScript 2 Answers

question mark in "setPixel" documentation example? 1 Answer

weird result when substracting/adding; 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