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:
From the main menu, select Assets > Create > Scripting > MonoBehaviour Script.
Name the script
Example.csand open it.To use the Input System API in the rest of your script, add the Input System's
usingdirective to the top of your script:using UnityEngine.InputSystem;In your class, create a variable of type
InputActionfor each action that you want to use in your script. A useful naming convention is to addActionto the name of the action. For example:InputAction moveAction; InputAction jumpAction;In your
Startmethod, useFindActionto find each action, and store the reference in its variable. Don't callFindActionin yourUpdatemethod, because it performs a string-based lookup, which might affect performance. For example:moveAction = InputSystem.actions.FindAction("Move"); jumpAction = InputSystem.actions.FindAction("Jump");In your
Updatemethod, 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();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
FindActionreturnsnull, the project-wide asset has no action with the name you specified. Check the action's name in the Input Actions Editor. - If calling
FindActionthrows aNullReferenceException,InputSystem.actionsisnullbecause 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:
Create a MonoBehaviour script, then add the Input System's
usingdirective to the top of the script.In your script, create a public field of type
InputActionReferencefor each action that you want to use. For example:// Assign these actions in the Inspector window. public InputActionReference moveAction; public InputActionReference jumpAction;In your
Updatemethod, read the value of each action through theactionproperty of its reference. For example:Vector2 moveValue = moveAction.action.ReadValue<Vector2>(); // Your movement code here. if (jumpAction.action.IsPressed()) { // Your jump code here. }Attach the script to a GameObject.
In the Inspector window, select the object picker (⊙) next to each field, and select the action that you want, such as Player/Move.
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.