Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Why isnt my else if statement working?

Discussion in 'Scripting' started by Abuscaglio, Apr 12, 2018.

  1. Abuscaglio

    Abuscaglio

    Joined:
    Sep 5, 2017
    Posts:
    46
    I created a settings/keybind script and when I click a button, I want the function to listen for a keystroke OR mouse click and then map the button or key to the buttons text field. It's working as intended for the keyboard but I cannot get it to recognize my mouse clicks and map the keycode to the buttons text... Can anyone provide some input?

    Code (CSharp):
    1. void OnGUI()
    2.     {
    3.         if (currentKey != null) {
    4.             //RCPanel.SetActive (true);
    5.             //foreach (Button button in InactiveButtons) {
    6.             //button.interactable = false;
    7.             Event e = Event.current;
    8.             if (e.isKey) {
    9.                 Keys [currentKey.name] = (e.keyCode);
    10.                 foreach (var kvp in Keys.ToList()) {
    11.                     if (kvp.Value == e.keyCode){
    12.                         if (currentKey.name != kvp.Key) {
    13.                             Keys [kvp.Key] = KeyCode.None;
    14.                             ButTextLookup [kvp.Key].text = "*Unbound*";
    15.                         } else if (currentKey != null) {
    16.                             if (mouseClicked.isMouse) {
    17.                                 if (mouseClicked.type == EventType.MouseDown) {
    18.                                     for (int i = 0; i < mouseButtons.Length; ++i) {
    19.                                         if (Input.GetKeyDown (mouseButtons [i])) {
    20.                                             Keys [currentKey.name] = (mouseButtons [i]);
    21.                                             foreach (var kvpb in Keys.ToList()) {
    22.                                                 if (kvpb.Value == mouseButtons [i]) {
    23.                                                     if (currentKey.name != kvpb.Key) {
    24.                                                         Keys [kvpb.Key] = KeyCode.None;
    25.                                                         ButTextLookup [kvpb.Key].text = "*Unbound*";
    26.                                                     }
    27.                                                 }
    28.                                             }
    29.                                         }
    30.                                     }
    31.                                 }
    32.                             }
    33.                         }
    34.                     }
    35.                 }
    36.  
    37.                 currentKey.transform.GetChild (0).GetComponent<Text> ().text = (e.keyCode.ToString());
    38.                 //currentKey.transform.GetChild (0).GetComponent<Text> ().text = mouseClicked[i].keycode.ToString ();
    39.                 currentKey = null;
    -"mouseClicked" is a new event I previously assigned
    -"mouseButtons" is an array containing six ceycodes for mouse buttons 1-6
    -"Keys" is a dictionary of keycodes populating the buttons text field on start
    -"ButTextLookup" is a dictionary containing all my buttons ui text elements
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You should not be checking for input in OnGUI(). All you should use OnGUI for is displaying a legacy GUI, nothing more. The legacy GUI system (today called Immediate Mode GUI or IMGUI) that OnGUI is part of is only for displaying debugging info or menus for the developer to see, and aren't intended for use by actual end users of the game.
     
  3. Abuscaglio

    Abuscaglio

    Joined:
    Sep 5, 2017
    Posts:
    46
    Would my function work as written on Update()?
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I don't know, so maybe give it a try. The way you've written it makes it extremely hard to read, so I haven't read through it.

    Running through all those for and foreach loops every update might not perform well though. If I was writing this I would split it up instead of nesting everything in one place just so it is easier to follow what you're doing. You should be able to easily split that up into half a dozen separate functions. That also makes it a lot easier to debug when looking for what may be going wrong.
     
    Last edited: Apr 12, 2018
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Agreed that you should split the code up into a few functions.
    Store the setting to which you're assigning a new keybind.
    Use the PopEvent I showed in another thread, or even get all of the enum values and loop them; just make sure the script is not active for the rest of the game.. I think it's no problem.

    When you get a value, call a method that works out the (re-)assignment, and let the UI be updated after all of that is done. :)

    I hope you get it resolved.
     
  6. FuguFirecracker

    FuguFirecracker

    Joined:
    Sep 20, 2011
    Posts:
    419
    @Abuscaglio

    Foreach loop inside a for loop inside a foreach lopp inside OnGUI... That code is insane!

    I think I know what you're trying to do... but it's a lot simpler than that.

    See if this turns on any lightbulbs:


    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class ParseInput : MonoBehaviour {
    4.  
    5.     protected void Update()
    6.     {
    7.         HandleInput();
    8.     }
    9.  
    10.     private void HandleInput()
    11.     {
    12.         if (Input.anyKeyDown)
    13.         {
    14.             Debug.Log(Input.inputString);
    15.         }
    16.    
    17.     }
    18.  
    19. }