Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Arashio · Oct 10, 2018 at 08:44 PM · if-statements

IF statement ignoring condition

So ive been working on a game where the player can shift its shape when press J, K, or L key. The problem is I want to restrict the player from pressing the same key twice so i code some if statement as below:-

 public GameObject sphere;
 public GameObject cube;
 //public GameObject pyramid;
 
 bool keypressJ = false;
 bool keypressK = true;
 
 void FixedUpdate()
     {
         if (Input.GetKeyDown(KeyCode.J) && keypressJ == false)
         {
             sphere.transform.position = cube.transform.position;
             sphere.SetActive(true);
             cube.SetActive(false);
             //pyramid.SetActive(false);
 
             keypressJ = true;
             keypressK = false;
 
             Debug.Log("keypress J = " + keypressJ);
         }
 
         if (Input.GetKeyDown(KeyCode.K) && keypressK == false)
         {
             cube.transform.position = sphere.transform.position;
             cube.SetActive(true);
             sphere.SetActive(false);
             //pyramid.SetActive(false);
 
             keypressJ = false;
             keypressK = true;
 
             Debug.Log("keypress J = " + keypressJ);
         }

when i run the game it run perfectly on the first shift. but after it returns to cube shape, i cant shift it back to sphere. i have look around for answer but still couldnt find any. this is my first time posting on the community so i hope my question is clear enough. any help is very much appreciated.

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 W01Fi3 · Oct 10, 2018 at 08:51 PM 0
Share

@Arashio I ran the script myself, and I get no errors. Is this script attached to one of the objects, or is it a separate object? If it's attached to one of the objects, what happens is that when you disable the object, all components stop as well, thus the script never runs again

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by hexagonius · Oct 10, 2018 at 08:53 PM

what about this solution: save the keycode pressed if valid and do the transformation. always check and who's the current saved keycode. that should already do it.
note: if you already have something that can be used for if checking, don't so additional bools you need to handle.

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 W01Fi3 · Oct 10, 2018 at 08:59 PM 0
Share

I ran the script myself and I got no errors; perhaps it isn't an actual scripting error.

avatar image
0

Answer by Bunny83 · Oct 10, 2018 at 09:16 PM

Do never check down or up events (or generally anything that is only true for one frame) inside FixedUpdate. Those things can only be done in Update. FixedUpdate does not necessarily run every frame and can miss such events.

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

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

94 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

How come my camera does not follow my script? 1 Answer

Why simple letter evaluataion returns true no matter witch letter 1 Answer

Coroutine freezes editor but only sometimes. Why ? Why is it happening randomly ? 3 Answers

Unity freezes after running this basic code. 2 Answers

How to call unity event once every time my object reach destination in Update()? 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