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
0
Question by Robindoo · Jan 14, 2011 at 04:52 AM · inputupdatebooleankeyboard

Keyboard Input Question

I'm testing out a script where by when the player presses a key "1" on the keyboard, it will disable a component. The script is as follow.

var c1 : GameObject;

var c1Boo : boolean = false;

function Update() { if(Input.GetKey("1") { if(!c1Boo) { c1Boo = true; CheckBoo(); }

     else if (c1Boo)
     {
         c1Boo = false;
         CheckBoo();
     }
 }

}

function CheckBoo() { if(c1Boo) { c1.GetComponent("MoveCube").enabled = false; }

 else if(!c1Boo)
 {
     c1.GetComponent("MoveCube").enabled = true;
 }

}

Correct me if my logic is wrong. In my Update(), if the player presses the key "1", it will check for the first condition which the c1Boo is false. Then it will turn c1Boo to true and activate CheckBoo() which will check for c1Boo condition. If that condition is true, c1 will be disabled. The CheckBoo should only run when the player presses "1". But I did a Debug.Log() and it shows that the CheckBoo is running for a few more frames before it stops.

When I used an OnGUI(), making the control a button, I don't face this problem. The gameObject is disabled when the player first clicks it and will be enabled when the player clicks again.

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
1
Best Answer

Answer by Jessy · Jan 14, 2011 at 05:01 AM

Your problem stems from the fact that you are using Input.GetKey instead of Input.GetKeyDown or Input.GetKeyUp.

Your entire script could be condensed to this:

var moveCube : MoveCube;

function Update() { if (Input.GetKeyDown("1")) moveCube.enabled = !moveCube.enabled; }

If you feel the need to use GetComponent, first, don't use a string if you can help it, and use the Generic version. The way you had it is unnecessarily slow. (Using a reference to the MoveScript, as in the previous code snippet, is faster still, than this following snippet):

c1.GetComponent.<MoveCube>().enabled
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 Robindoo · Jan 14, 2011 at 05:21 AM 0
Share

the reason i had to use getComponent is because I had this script attach to the camera and moveCube attach to another object. I'm currently working it this way: having 4 different game objects, each of them having been attached to a moveCube script.

avatar image Robindoo · Jan 14, 2011 at 05:25 AM 0
Share

oh and thanks, it works. Just one question what is the difference between using GetComponent with a string in it and using the Generic Version as u mentioned.

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

No one has followed this question yet.

Related Questions

Reading input outside of Update or some other ticking system 2 Answers

Unable to catch KeyCode.LeftShift Down AND Up 5 Answers

control key question 1 Answer

Mouse Input for RayCast - Update or FixedUpdate 1 Answer

How to make floating mobile keyboard in mobile game ? 0 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