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 /
  • Help Room /
avatar image
0
Question by Extrapafurdio · Sep 12, 2015 at 08:31 PM · inputsetactiveinput.getkey

How to use the same input for two different actions without them happening simultaneously.

This is a simple problem, but I just can't seem to find a solution: what I want is, when I press, to deactivate the page, to instantaneously activate the bigPage, then, when I press E again, the bigPage is deactivated. But, actually, when E is pressed, everything happens at the same time: page deactivates, bigPage activates and deactivates.

So I wish to find a way to separete these actions so they don't happen simultaneously, still using E as input for both. This is the code.

 public GameObject bigPage;
 private bool haveRead = false;

 void OnTriggerStay2D(Collider2D page)
 {
     if (page.gameObject.CompareTag ("Collectible")) 
     {
         if (Input.GetKeyDown(KeyCode.E) && haveRead == false)
         {
             page.gameObject.SetActive(false);
             bigPage.gameObject.SetActive(true);
             haveRead = true;
         }
         if (Input.GetKeyDown(KeyCode.E) && haveRead == true)
         {
             bigPage.SetActive(false);
             haveRead = false;
         }
     }
 }

Note that I tried putting some booleans, but they don't do anything (because their values are also changed simultaneously).

If I change any if (Input.GetKeyDown(KeyCode.E))to any other key, it works properly. But I want both to be E.

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 Nephtis · Mar 18, 2018 at 11:35 AM 0
Share

Well, the second "if" is always going to happen after the first one does because you set haveRead to true inside the first "if", which will then mean it's true in the second "if" check, thus triggering it. Try replacing it with else if.

Remember that this is all happening within the same frame.

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Rostam24 · Sep 12, 2015 at 08:37 PM

Try again with:

 else if (Input.GetKeyDown(KeyCode.E) && haveRead == true)
  {
         bigPage.SetActive(false);
         haveRead = false;
  }
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 Extrapafurdio · Sep 12, 2015 at 08:55 PM 0
Share

It doesn't work, because I think the problem remains: when I press E, the page is deactivated, and the bigPage is activated and deactivated.

avatar image Rostam24 Extrapafurdio · Sep 12, 2015 at 08:59 PM 0
Share

One more try then:

 if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E) 
 {
          if(haveRead) *edit*: this should be: (!haveRead)
          {
              page.gameObject.SetActive(false);
              bigPage.gameObject.SetActive(true);
              haveRead = true;
          }
         else 
          {
              bigPage.SetActive(false);
              haveRead = false;
          }
 }
avatar image Extrapafurdio Rostam24 · Sep 12, 2015 at 09:05 PM 0
Share

Still doesn't. Now I think that the bigPage is immediately deactivated because if any other thing happens, it will get deactivated, and many other things happen. >:

Show more comments
avatar image
0

Answer by getyour411 · Sep 12, 2015 at 08:38 PM

 if(key)
   if(haveRead == false)
    // do stuff
   ELSE // which means haveRead is true)
   // do the other stuff

The problem with the way you have it written is that when it's false it does stuff, sets haveRead to true, then moves to the second if block and finds haveReads is true so it does stuff again (and again...)

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 Extrapafurdio · Sep 12, 2015 at 08:54 PM 0
Share

Yup, that's exactly what I have written, and don't know how to get out of it. I'm sorry, but I didn't really understand your code. >:

avatar image
0

Answer by Tatanchis · Jun 04, 2018 at 09:38 PM

I know it is late to answer this question but i was looking for the same and i found myself this solution:

 private int number = 1;
 void Update(){
   if (Input.GetKeyUp(KeyCode.Z) && (number%2) == 1){
             //Your actions here
             number = number + 1;
         }else if (Input.GetKeyUp(KeyCode.Z) && (number % 2) == 0)
         {
             //Your other actions here
             number = number - 1;
         }
 }

As there are only two possible conditions we just see if the number is odd or not and modify the number depending on the condition. I hope this helps someone, cheers :)

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
0

Answer by kami1339 · Feb 02, 2019 at 05:09 PM

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class EnableDisableOtherscript : MonoBehaviour { public GameObject cam1; public GameObject cam2; private bool haveRead = false; // Use this for initialization

 void Start ()
 {
     

 }
 void Update ()

 { 

     if (Input.GetKeyDown(KeyCode.E) && haveRead == false)
     {
     cam1.SetActive(false);
         cam2.SetActive(true);
         haveRead = true;
         return;
     }
     if (Input.GetKeyDown(KeyCode.E) && haveRead ==true)
     {
         cam1.SetActive(true);
         cam2.SetActive(false);
         haveRead =false;
     
     }


 }

}

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

31 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

Related Questions

Shift Input.GetKey returns true even if button no longer held down on Mac WebGL if Shift+Command+4 is pressed (screenshot) 0 Answers

how we exit of my project with twice pressing back button??? 3 Answers

Get input positive name 0 Answers

if (input.Getkey(Keycode) returns true forever after pressing the key once? 1 Answer

Why does Input.GetKey(KeyCode.LeftControl) returns true when I press right alt? 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