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
1
Question by Henk Jan Baard · Jun 21, 2010 at 09:51 AM · keyboardkeycodegetkeyup

GetKeyUp fires twice?!?

I have a script listening if the user presses a key.

void OnGUI()
{
    if (Input.GetKeyUp(KeyCode.C))
    {
        print("c pressed");
    }
}

The problem is that 'c pressed' is printed twice when the user releases the "c" key. Is this a bug or do I do something wrong here?

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 Fattie · Apr 03, 2014 at 05:41 PM 0
Share

you must use Update(), not OnGUI or anything else

3 Replies

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

Answer by Mike 3 · Jun 21, 2010 at 09:54 AM

OnGUI is called multiple times a frame - a minimum of twice (except on iphone where you can disable the layout event)

Use something like this if you need to use OnGUI:

if (Event.current.type == EventType.KeyUp && Event.current.keyCode == KeyCode.C)
{
    print("c pressed");
}
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 Henk Jan Baard · Jun 23, 2010 at 07:58 AM 0
Share

What I did was using the Update function, but thanks for explaining the OnGUI function.

avatar image BerggreenDK · Jul 27, 2011 at 01:14 PM 0
Share

Found out that my function wasnt called in Update as I thought, it was way down in a GUI call. Once I got it moved to Update my problem was solved too.

avatar image
1
Best Answer

Answer by Cyclops · Jun 21, 2010 at 02:26 PM

Personally, I prefer to move my input-checking to the Update() function, like so (C# ahead):

bool quitMenu = false;

void Update () { CheckKeys(); } void CheckKeys() { if (Input.GetKeyUp(KeyCode.Escape)) { quitMenu = !quitMenu; } }

By setting a boolean variable, I can then display a Menu in OnGUI() based on quitMenu.

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 BerggreenDK · Jul 26, 2011 at 11:02 PM 0
Share

strange, I have tried to build it with Update and a counter, the counter ticks twice within Update too ?

avatar image bracciata · Sep 20, 2015 at 11:12 PM 0
Share

So can you explain because I had it in there and thinking that having all of those if statements for every input would slow down the game so ins$$anonymous$$d i tried on gui, will that slow it down more.

avatar image
2

Answer by Jason B · Jul 26, 2011 at 11:33 PM

Since you're saying that it even gets called twice inside of Update, the problem clearly seems to be that you have two objects in your scene running this same script at the same time. I'm not sure how that would have happened, but it seems the most likely culprit.

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 JimmyPOO · Jan 06, 2012 at 08:32 AM 0
Share

seems to be firing twice inside my update function also, here is a melee damage script i am using:

function Update(){

if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.F)){

 $$anonymous$$eleeAttack();

  

}

}

function $$anonymous$$eleeAttack(){

 var colls: Collider[] = Physics.OverlapSphere(transform.position, range);

 for (var hit : Collider in colls) {

   if (hit && hit.tag == "Enemy"){ 

      hit.Send$$anonymous$$essage("ApplyDamage", meleeDamage);

  

 }

 }

}

Attach this to a game object,. then it send apply damage to the following on the object it wants to damage.

function ApplyDamage(damage :float){

if (health > 0){

 health -= damage;

 healthBarLength -= damage;

 }

 if (health <= 0){

   remove$$anonymous$$e();



 }

}

any ideas? if I uncheck the melle damage script while it is running it does no damage, if i check it, it does twice the damage it should.

avatar image bregu · Feb 17, 2015 at 01:06 PM 0
Share

had the same issue and in my case I found that the script was attached twice once to SceneController object and to my terrain :/ quick way of finding out if that's the case -> detach the script from the object that the script should be attached to and test if the script is still executing if it does well clearly it is attached somewhere else as well ;)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Panel isn't opening after hitting the tab key, and its not even saying I pressed the key? 0 Answers

How to get Key with KeyCode.Question? 1 Answer

How to use event.KeyCode 1 Answer

Stimulate key press via a GUITexture 0 Answers

i can assign all keys but the shift any ideas 4 Answers


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