• 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 Vuzok · Jul 18, 2016 at 08:43 AM · if-statementsifgetkeydowngetkeygetkeyup

Update GetKey If Statements both playing instantly

I am trying to turn on and off an indicator, I've tried to set it up so that if the left indicator is on and they turn on the right one the left turns off and the right turns on. This part works fine but then I am trying to get it so that if they press the right key (E) when the right indicator is already on it will turn off. Its all inside an update function and my problem is that the on and off parts are both playing instantly and the indicator is either turning off and back on again or on and back off again immediately. How can I prevent this? I have already put in a float that will only equal 1 when indicator is on but the instant it equals one the off if statement triggers. I'm very very confused thanks in advance for any help!

 using UnityEngine;
 using System.Collections;
 
 public class Indicator : MonoBehaviour
 {
 
     public float indicator;
 
     public Renderer indicatorLeft;
     public Renderer indicatorRight;
 
     void Start()
     {
         indicator = 0;
     }
 
     void Update()
     {
 
         if (Input.GetKeyUp(KeyCode.Q))
         {
 
             indicator = 1;
 
             //This part turns on the left indicator.
             indicatorLeft.enabled = true;
             GameObject.FindWithTag("IndicatorLeft").GetComponent<AudioSource>().mute = false;
             print("Indicating Left");
             //This part turns off the right indicator.
             indicatorRight.enabled = false;
             GameObject.FindWithTag("IndicatorRight").GetComponent<AudioSource>().mute = true;
             print("Right Indicator Off");
         }
 
         if (Input.GetKeyUp(KeyCode.E))
         {
             indicator = 2;
 
             //This part turns on the right indicator.
             indicatorRight.enabled = true;
             GameObject.FindWithTag("IndicatorRight").GetComponent<AudioSource>().mute = false;
             print("Indicating Left");
             //This part turns off the left indicator.
             indicatorLeft.enabled = false;
             GameObject.FindWithTag("IndicatorLeft").GetComponent<AudioSource>().mute = true;
             print("Indicating Right");
         }
 
        if (indicator == 2 && Input.GetKeyDown(KeyCode.E))
         {
             indicator = 0;
             //This part turns off the right indicator.
             indicatorRight.enabled = false;
             GameObject.FindWithTag("IndicatorRight").GetComponent<AudioSource>().mute = true;
             print("Right Indicator Off");
         }
 
        if (indicator == 1 && Input.GetKeyDown(KeyCode.Q))
         {
             indicator = 0;
             //This part turns off the left indicator.
             indicatorLeft.enabled = false;
             GameObject.FindWithTag("IndicatorLeft").GetComponent<AudioSource>().mute = true;
             print("Left Indicator Off");
         }
     }
 }
 
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

1 Reply

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

Answer by saschandroid · Jul 18, 2016 at 09:30 AM

     private void Update () 
     {
         if (Input.GetKeyDown(KeyCode.Q))
         {
             if (!indicatorLeft.enabled)
             {
                 indicatorLeft.enabled = true;
                 indicatorRight.enabled = false;
             }
             else
             {
                 indicatorLeft.enabled = false;
             }
         }
     
         if ( Input.GetKeyDown(KeyCode.E))
         {
             if (!indicatorRight.enabled)
             {
                 indicatorLeft.enabled = false;
                 indicatorRight.enabled = true;
             }
             else
             {
                 indicatorRight.enabled = false;
             }
         }
     }


Is it this what you want?

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 Vuzok · Jul 18, 2016 at 09:33 AM 0
Share

Exactly what I wanted, thank you for helping me out!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

48 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

Related Questions

Can/how do I set up a boolean to continue an animation after button press 1 Answer

Dialogue with GetKey - Codes 1 Answer

How to get holding key to prioritize 1 Answer

Change view if key is pressed 4 Answers

Input.GetKey question 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges