Search Unity

Input.GetKey("Alpha1") error, Noob question

Discussion in 'Scripting' started by Mortalanimal, Oct 17, 2019.

  1. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    567
    Probably a simple answer. lol


    Code (CSharp):
    1. if (Input.GetKey("left ctrl") && Input.GetKey("Alpha1"))
    2.         {
    3.             Debug.Log("1");
    4.         }
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    There's a list of key names that Unity recognizes near the bottom of this page. I'm not sure what "Alpha1" is, but it's not on the list.

    If you are trying to get an input that you defined in the input manager, you probably want to be using GetButton or GetAxis instead of GetKey.
     
  3. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    567
  4. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    567
    @Antistone, i got the same Error for


    Code (CSharp):
    1. if (Input.GetKey("left ctrl") && Input.GetButton("Alpha1"))
    2.         {
    3.             Debug.Log("1");
    4.         }
     
  5. Giovane

    Giovane

    Joined:
    Sep 23, 2019
    Posts:
    36
    try
    Code (CSharp):
    1.     if (Input.GetKey(KeyCode.LeftCtrol) && Input.GetKey(KeyCode.Alpha1)
    2.             {
    3.                 Debug.Log("1");
    4.             }
    5.  
     
  6. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    567
  7. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    567
  8. Giovane

    Giovane

    Joined:
    Sep 23, 2019
    Posts:
    36
  9. Giovane

    Giovane

    Joined:
    Sep 23, 2019
    Posts:
    36
  10. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    567
    @Giovane, @Antistone, Ok so the Error Is gone when I use the following Code, However, The Debug.Log doesnt trigger, furthermore, if I press Ctrl+1 in play mode, something weird happens with my camera view!

    Code (CSharp):
    1. if (Input.GetKey("left ctrl") && Input.GetKey(KeyCode.Alpha1))
    2.         {
    3.             Debug.Log("1");
    4.         }
     
  11. Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Test : MonoBehaviour
    4. {
    5.     void Update()
    6.     {
    7.         if (Input.GetKey(KeyCode.LeftControl) )
    8.         {
    9.             Debug.Log("LeftControl");
    10.         }
    11.         if (Input.GetKey(KeyCode.Alpha1) )
    12.         {
    13.             Debug.Log("Alpha1");
    14.         }
    15.         if (Input.GetKey(KeyCode.Alpha1) && Input.GetKey(KeyCode.LeftControl))
    16.         {
    17.             Debug.Log("LeftControl + Alpha1");
    18.         }
    19.     }
    20. }
    21.  
    thread.png
     
  12. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    567
    @Lurking-Ninja, Thx bro, I noticed However, It Only works if I hold "1" first then press "Ctrl"

    I kinda need it the other way around. If I press Ctrl first it doesnt work for me. generally in RTS game for players to store units they press Ctrl first then the number


    Code (CSharp):
    1. if (Input.GetKey("left ctrl") && Input.GetKey(KeyCode.Alpha1))
    2.         {
    3.             Debug.Log("1");
    4.         }
    5.  
    6.         if (Input.GetKey(KeyCode.Alpha1) && Input.GetKey(KeyCode.LeftControl))
    7.         {
    8.             Debug.Log("LeftControl + Alpha1");
    9.         }

    https://puu.sh/Ets6S/0ca88418bf.png
     
  13. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    567
    Lol I just noticed that Ctrl+1 and Ctrl+2 changed between scene wind and game window
     
  14. Use only what I gave you. Delete your string-based stuff, it is not working.
    What I gave you should work every way around.
    thread.png

    If you take a look at the numbers on the right side, I held the Left CTRL, then I hit the 1.

    Oh, and don't try it in Editor (or try it at least 2019.something) unless you have the Shortcuts feature and you can remove the CTRL-1 shortcut to switch to the Scene view.
     
  15. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    567
    Lurking-Ninja likes this.
  16. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    567
    Both Functions work now after i removed those shortcuts