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 /
avatar image
0
Question by Arkos25 · May 08, 2018 at 07:30 PM · scripting problemprogrammingbuttons

Using keyboard to controll UI buttons

Hey guys, so as you may already know I want to be able to use wasd or arrow keys to move between buttons but with my code I just can't seem to be able to do more than invoke only one button.

""

As you can see in my code below I've used the images for each button as a marker and am using those to move but then I'm calling the actual button in order to invoke the onClick(); once they are in their correct... position?.. I'm basically saying if image[2] then invoke this buttons onClick(); but it doesn't. Not fully at least, the first if gets called correctly but none of the others do, they all invoke the same button and I can not, for the love of me, understand why.

""

mostly because I've only been coding for a week and a half so I'm quite new to this. Anyway I could really use some help here. What am I missing? The thing is if I comment out the first if then the second if is called and we are back inside that god forsaken loop where all the buttons call the same onClick();

""

I am THIS close to ripping my hair out, please stop me.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 
 
 public class ButtonManager : MonoBehaviour {
     //References for each menu
     //Pause//
     public CanvasGroup canvaspause;
 
     //WinScreen//
 
     //Array
     public Image[] image;
     public Button[] buttons;
 
     //Button selection
     private int selected;
 
     //Color selection
     Color normal;
     Color highlighted;
     //Color Pressed;
 
     //Bool
     private bool Paused = false;
 
 
     // Use this for initialization
     void Start () { 
 
         normal = new Color32(0xEC, 0xEC, 0xEC, 0xFF);
         highlighted = new Color32(0xFF, 0xFF, 0xFF, 0xFF);
         //Pressed = new Color32(0x85, 0x85, 0x85, 0xFF);
     }
 
 
     public void MoveToNextButton()
     {
         image[selected].color = normal;
         selected++;
         if (selected > 2)
         {
             selected -= 1;
         }
         image[selected].color = highlighted;
     }
 
     public void MoveToPreviousButton()
     {
         image[selected].color = normal;
         selected--;
         if (selected < 0)
         {
             selected += 1;
         }
         image[selected].color = highlighted;
     }
 
     // Update is called once per frame
     void Update () {
         if(canvaspause.alpha >= 1f)
         {
             Paused = true;
         }
 
         if(Paused && Input.GetKeyDown(KeyCode.S))
         {
             MoveToNextButton();
         }
         else if (Paused && Input.GetKeyDown(KeyCode.W))
         {
             MoveToPreviousButton();
         }
 
         if(Paused && Input.GetKeyUp(KeyCode.E))
         {
             if (image[0])
             {
                 buttons[0].onClick.Invoke();
             }
             else if (image[1])
             {
                 buttons[1].onClick.Invoke();
             }
             else if (image[2])
             {
                 buttons[2].onClick.Invoke();
             }
         }
     }
 }
 
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 Arkos25 · May 09, 2018 at 12:22 PM

Okay Guys!

Found the answer to this question so for anyone that is struggling you can find my solution to this problem here. I'll mark the changes I did from the script above. I did two things differently on my code below, one, I created a second variable in order to keep track of my buttons and two I added this line

""

buttons[selected].FindSelectable(Vector2.down); >> (inside MoveToNextButton(); ) ""

and

buttons[selected].FindSelectable(Vector2.Up); >> (inside MoveToPreviousButton();)

""

These were then called by pressing either S or W inside the update.

""

     //Button selection
     **__private int selected;__**
     **__private int color;__**
 
     //Color selection
     Color normal;
     Color highlighted;
     //Color Pressed;
 
     //Bool
     private bool Paused = false;
 
 
     // Use this for initialization
     void Start () {
         selected = 0;
         color = 0;
         normal = new Color32(0xEC, 0xEC, 0xEC, 0xFF);
         highlighted = new Color32(0xFF, 0xFF, 0xFF, 0xFF);
         //Pressed = new Color32(0x85, 0x85, 0x85, 0xFF);
     }
 
 
     public void MoveToNextButton()
     {
         image[color].color = normal;
         **buttons[selected].FindSelectable(Vector2.down);**
         **selected++;**
         **color++;**
         if **(**selected > 2 && color > 2**)**
         {
             color -= 1;
             selected -= 1;
         }
         image[color].color = highlighted;
     }
 
     public void MoveToPreviousButton()
     {
         image[color].color = normal;
         ****buttons[selected].FindSelectable(Vector2.up);****
         selected--;
         color--;
         if ****(selected < 0 && color < 0)****
         {
             color += 1;
             selected += 1;
         }
         image[color].color = highlighted;
     }
 
     // Update is called once per frame
     void Update () {
         if(canvaspause.alpha >= 1f)
         {
             Paused = true;
         }
 
         if(Paused && Input.GetKeyDown(KeyCode.S))
         {
             MoveToNextButton();
         }
         else if (Paused && Input.GetKeyDown(KeyCode.W))
         {
             MoveToPreviousButton();
         }
 
         if(Paused && Input.GetKeyUp(KeyCode.E))
         {
             if **(**selected == 0**)**
             {
                 buttons[0].onClick.Invoke();
             }
             else if (**selected == 1**)
             {
                 buttons[1].onClick.Invoke();
             }
             else if (**selected == 2**)
             {
                 buttons[2].onClick.Invoke();
             }
         }
     }

""

Sorry of the shitty explanation so if you didn't understand, write the code yourselves and try it out. Learn by doing I guess :D

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

151 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

[Compute Shader] Pixel Data from Texture Coordinates 0 Answers

Is there a way to disable something permanently in a script? 2 Answers

How can i get current camera's background color? 1 Answer

On raycast hit remove Rigidbody 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