• 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
Question by WesterlyCarrot9 · Dec 12, 2013 at 01:11 PM · c#javascriptguibuttonkey

Key for GUI.Button

I'm so frustrated so i hope you good people in the community can help :P I want to make my two GUI.Buttons from the code below to activate with keys and i've done it before in simpler scripts but it just won't work here! Any help? Thanks!

 var Pressed : boolean = false;
 var charPressed : boolean = false;
 
 var guiRatio : float;
 var sWidth : float;
 var GUIsF : Vector3;
 
 var cube : GameObject;
 var check : boolean = false;
 
 function Awake(){
     sWidth = Screen.width;
     guiRatio = sWidth / 1440;
     GUIsF = new Vector3(guiRatio,guiRatio,1);
 }
 
 function Update(){
     if(cube == null){
         check = true;
     }    
 }                
 
 function OnGUI(){
         if(check){
                 GUI.matrix = Matrix4x4.TRS(new Vector3(Screen.width - 100*GUIsF.x,Screen.height - 60*GUIsF.y,0),Quaternion.identity,GUIsF);
                 if(GUI.Button(new Rect(-20,-20,100,60),"Deer")){
                     gameObject.GetComponent(CharacterMotor).jumping.baseHeight = gameObject.GetComponent(CharacterMotor).jumping.baseHeight + 3;
                     Pressed = true;
                         if(gameObject.GetComponent(CharacterMotor).jumping.baseHeight >= 4){
                             gameObject.GetComponent(CharacterMotor).jumping.baseHeight = 4;
                         }
                     }
                 }                                                                                    
                 if(Pressed){
                     GUI.matrix = Matrix4x4.TRS(new Vector3(Screen.width - 100*GUIsF.x,Screen.height - 60*GUIsF.y,0),Quaternion.identity,GUIsF);
                     if(GUI.Button(new Rect(-20,-100,100,60),"Stop")){
                         gameObject.GetComponent(CharacterMotor).jumping.baseHeight = 1;
                         Pressed = !Pressed;
                     }
                 }
             }    

     
Comment

People who like this

0 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 KellyThomas · Dec 12, 2013 at 01:41 PM 0
Share

What is happening when you run this code?

2 Replies

  • Sort: 
avatar image
Best Answer

Answer by premier · Dec 12, 2013 at 01:44 PM

On GUI.Button you have option to set a tooltip (good for hover effects) or set ID parameter! You can use ID for any button you have.

In my scripts every time i click in gui.button i send a message for all scripts who extends my other script, like this:

 Events.ToList().ForEach(evt => evt.OnClickUIButton(ID));

Then in other script i have:

 public void OnClickUIButton(string ID) {
     switch(ID) {
         case "PlayGame":
             // do same action
             break;
     }
 }
Comment
iwaldrop

People who like this

1 Show 0 · 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

Answer by WesterlyCarrot9 · Dec 12, 2013 at 01:48 PM

Nevermind guys i just figured out what need to be done :P Thanks for your replies anyway :P

Comment
robhuhn
iwaldrop

People who like this

-2 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 iwaldrop · Dec 12, 2013 at 05:02 PM 0
Share

You really should have accepted the answer by premier, or at least detailed your solution. This is a community site for archiving answers.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

Assign a hotkey to a GUI button? 3 Answers

Luz parpadeante con bateria 0 Answers

How to make a or array of GUI.Button's? 1 Answer

How to make a box appear after pressing a button? 1 Answer

Button OnClick Applied To Whole Canvas? 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