Menu
CoddyTech

Named Buttons

Part of the Unity Scripting Basics section of Coddy's Unity journey. Lesson 14 of 67.

Besides keys, Unity has buttons: names listed in the Input Manager (Edit, Project Settings, Input Manager), each tied to one or more keys. Input.GetButtonDown("Fire1") asks about the name, not about a key. A new project has these:

  • "Jump": Space
  • "Fire1": left Ctrl or the left mouse button
  • "Fire2": left Alt or the right mouse button
  • "Submit" and "Cancel": Enter and Escape

Gamepad buttons are tied to the same names, and the keys can be changed in the Input Manager without touching the code. GetButton, GetButtonDown and GetButtonUp work like GetKey, GetKeyDown and GetKeyUp. This blaster logs a shot for every Ctrl press or click:

void Update()
{
    if (Input.GetButtonDown("Fire1"))
    {
        Debug.Log("Shot!");
    }
}

The name is a string, so a misspelled name compiles. Unity looks it up when the line runs, and when no button has that name it throws on every frame: ArgumentException: Input Button Fier1 is not setup. When you see that message, compare the name in your code with the Input Manager's list.

challenge icon

Challenge

Easy

The frog hops while the Jump button is held: pressing Jump lifts it by hop units once, and releasing Jump puts it back down by the same amount. The starter already does the lifting, but it throws an error as soon as the game runs.

Press Play and read the error in the Console. Fix it, add the landing on release, then press Check.

Try it yourself

using UnityEngine;

public class Hopper : MonoBehaviour
{
    public float hop = 1.5f;

    void Update()
    {
        if (Input.GetButtonDown("Jumpp"))
        {
            transform.position += new Vector3(0, hop, 0);
        }
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Unity Scripting Basics