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 DonGato · Feb 18, 2011 at 04:05 AM · javascriptif-statementsoperator

EXTREMELY basic scripting question.

I feel stupid.

What's wrong with this code:

function Start () {
    if ((whichTeam != 1) || (whichTeam != 2)) {
        Debug.Log ("Invalid team value");
    }
}

It doesn't work, just writes Invalid team value to the log every time I hit play, no matter what the value of whichTeam is. I am confused. Seems to work if I change the !='s to =='s, and also works if I just put (whichTeam != 1)

Easy to work around, but I just want to know, what's wrong with this? Or is Unity incapable of handling that?

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 simplyRubbish · Feb 18, 2011 at 04:11 AM

What are the possible values which team can be? If it's 1 or 2 then it will always go into there because the OR operator. If which team is equal to 1 the second condition will let it pass as true and vice versa.

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 DonGato · Feb 18, 2011 at 04:20 AM 0
Share

Duh. Now I feel really stupid. I guess I need to use &&.

I am new to scripting, and I was thinking in terms of how I would describe it to someone using language, not using math. I need to get used to thinking of scripting more as math, despite it's basis in language. (obviously).

Thanks for taking the time to set me straight.

avatar image simplyRubbish · Feb 18, 2011 at 04:36 AM 0
Share

No worries, we've all been there and I myself and many others have made that exact mistake. Live and learn I always say.

avatar image
2
Best Answer

Answer by tertle · Feb 18, 2011 at 04:14 AM

Your code makes no sense, it'll enter the if statement for whatever value of whichTeam.

if whichTeam is equal to 1 then whichTeam != 2 therefore enters if statement

if whichTeam is equal to 2 then whichTeam != 1 therefore enters if statement

if whichTeam is equal to another value then whichTeam != 1 && whichTeam != 2 (both statements true) therefore enters if statement

Every case enters if statement.

If you want it to only enter the statement when whichteam doesn't equal 1 or 2, then you want.

(whichTeam != 1) && (whichTeam != 2)

or

!((whichTeam == 1) || (whichTeam == 2))
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 DonGato · Feb 18, 2011 at 04:23 AM 0
Share

should be able to mark two answers as correct, you wrote in when I was typing my response to the other...

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

really strange thing with "if" statment not working well 1 Answer

Input.GetMouseButtonDown(0) running through my if statments too quickly 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Object not moving on KeyPress? 1 Answer

If...else statement with errors 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