docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Get references to actions in scripts

    Find actions by name or reference them in the Inspector window, so you can read their values in your scripts.

    To read an action in a script, you need a reference to the action. You can find the action by name with the FindAction method, or assign it in the Inspector window with an InputActionReference field.

    Finding actions by name needs no setup in the Inspector window, but you must update your code if you rename an action. A reference from the Inspector window still works if you rename the action, and you can select a different action without changing your code.

    After you get a reference, you can read the action's value every frame, a technique called polling, or set callbacks on the action.

    The Input System enables the actions in the project-wide actions asset automatically. If you use actions from a different action asset, enable them before you read them. For more information, refer to Enable actions.

    Find actions by name

    InputSystem.actions is the project-wide actions asset. To find actions in a different asset, call FindAction on a reference to that InputActionAsset instead.

    To find actions by name and read their values in a script:

    1. From the main menu, select Assets > Create > Scripting > MonoBehaviour Script.

    2. Name the script Example.cs and open it.

    3. To use the Input System API in the rest of your script, add the Input System's using directive to the top of your script:

      using UnityEngine.InputSystem;
      
    4. In your class, create a variable of type InputAction for each action that you want to use in your script. A useful naming convention is to add Action to the name of the action. For example:

      InputAction moveAction;
      InputAction jumpAction;
      
    5. In your Start method, use FindAction to find each action, and store the reference in its variable. Don't call FindAction in your Update method, because it performs a string-based lookup, which might affect performance. For example:

      moveAction = InputSystem.actions.FindAction("Move");
      jumpAction = InputSystem.actions.FindAction("Jump");
      
    6. In your Update method, read the values of your action variables.

      The way you read a value depends on the action's value type. For example, some actions return a 1D or 2D axis value, and others return a Boolean value. In this example, the Move action returns a 2D axis, and the Jump action returns a Boolean.

      Vector2 moveValue = moveAction.ReadValue<Vector2>();
      bool jumpValue = jumpAction.IsPressed();
      
    7. Attach the script to a GameObject, then enter Play mode.

    When you enter Play mode, the script reads the values of the Move and Jump actions every frame. If it doesn't, check the following:

    • If FindAction returns null, the project-wide asset has no action with the name you specified. Check the action's name in the Input Actions Editor.
    • If calling FindAction throws a NullReferenceException, InputSystem.actions is null because no action asset is assigned as project-wide. For more information, refer to Assign an existing action asset as project-wide.

    The following example script combines all these steps:

    using UnityEngine;
    using UnityEngine.InputSystem;
    
    /// <summary>
    /// Example script demonstrating the full actions-based workflow.
    /// </summary>
    public class Example : MonoBehaviour
    {
        // Store the action references.
        InputAction moveAction;
        InputAction jumpAction;
    
        private void Start()
        {
            // Find the references to the "Move" and "Jump" actions
            moveAction = InputSystem.actions.FindAction("Move");
            jumpAction = InputSystem.actions.FindAction("Jump");
        }
    
        void Update()
        {
            // Read the "Move" action value, which is a 2D vector
            // and the "Jump" action state, which is a boolean value
    
            Vector2 moveValue = moveAction.ReadValue<Vector2>();
            // Your movement code here.
    
            if (jumpAction.IsPressed())
            {
                // Your jump code here.
            }
        }
    }
    

    Reference actions in the Inspector

    To reference actions in the Inspector window and read their values:

    1. Create a MonoBehaviour script, then add the Input System's using directive to the top of the script.

    2. In your script, create a public field of type InputActionReference for each action that you want to use. For example:

      // Assign these actions in the Inspector window.
      public InputActionReference moveAction;
      public InputActionReference jumpAction;
      
    3. In your Update method, read the value of each action through the action property of its reference. For example:

      Vector2 moveValue = moveAction.action.ReadValue<Vector2>();
      // Your movement code here.
      
      if (jumpAction.action.IsPressed())
      {
          // Your jump code here.
      }
      
    4. Attach the script to a GameObject.

    5. In the Inspector window, select the object picker (⊙) next to each field, and select the action that you want, such as Player/Move.

    6. Enter Play mode.

    When you enter Play mode, the script reads the values of the actions that you selected every frame. If a field has no action assigned, reading it throws a NullReferenceException.

    Additional resources

    • Actions workflow
    • Input System workflows comparison
    • Polling actions
    • Set callbacks on actions
    • Enable actions
    In This Article
    Back to top
    Copyright © 2026 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)