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 /
This question was closed Nov 22, 2012 at 03:24 PM by Coreyf716 for the following reason:

Dumb question :D

avatar image
0
Question by Coreyf716 · Nov 22, 2012 at 03:14 AM · javascriptbooleanifelseconditional

Else and If

This is probably a stupid question, but I can't seem to understand it. I am at an intermediate level of programming and I've used if many times before, but this one confuses me. If I have a conditional statement:

 if (Input.GetKey(KeyCode.Escape /* Example*/) && condition1 && condition2) {
      whatever = true;
 } else {
      whatever = false;
 }

Even though my conditions are not false and escape has been let up, my boolean 'whatever' is set to false. I don't understand how the conditions can remain true and 'whatever' is set to false. Can anyone explain?

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 YM_Industries · Nov 22, 2012 at 05:29 AM 0
Share

This code should definitely set whatever to true. Could you post your actual code?

avatar image Coreyf716 · Nov 22, 2012 at 03:19 PM 0
Share

It does set whatever true. I understand now. That's what happens when you question yourself :)

2 Replies

  • Sort: 
avatar image
1

Answer by CodeMasterMike · Nov 22, 2012 at 06:24 AM

The Input.GetKey only returns true when its pressed down, so in your case the whatever variable is set to false when the Escape key is not pressed down.

Here is the documentation for GetKey: http://docs.unity3d.com/Documentation/ScriptReference/Input.GetKey.html

Good luck!

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

Answer by DeveshPandey · Nov 22, 2012 at 07:30 AM

I'm describing flow of code, have a look-
- when you pressed a 'Esc' key then 'whatever' goes true
- when you release 'Esc' key then 'whatever' goes false, because Input.GetKey(KeyCode.Escape) returns false.

I don't know what you want to do but try this, may be help you:

if ((Input.GetKey(KeyCode.Escape / Example/) && condition1 && condition2) || !whatever) {
whatever = true;
}
else {
whatever = false;
}

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 YM_Industries · Nov 22, 2012 at 07:35 AM 0
Share

I don't fully understand the purpose of this code. If whatever is false, it gets set to true. If whatever is true, it gets set to false, unless the escape key is pressed, in which case it stays true.

avatar image Coreyf716 · Nov 22, 2012 at 03:21 PM 0
Share

That would make whatever remain true while the game is running.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

Problem with On/Off Switch 1 Answer

If...else statement with errors 1 Answer

Use of boolean functions in c# 1 Answer

Better way to delay a function for a few seconds? Javascript 1 Answer

Button if else statement? 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