Scripting Actions (New Unity Input System)
Now that we have a solid understanding of delegates and events, it’s time to return to the new Unity Input System. The reason for our brief “detour” will become clear as we dive into today’s documentation
Objective: Learn how to script actions using the new Unity input system
Gaining Access via Script
Generating a C# Class
To gain access to the new Input System, select your Input Actions asset from the Project folder and click “Generate C# Class”.
This will create a new script containing a large amount of code related to the Input System. While we won’t use this script directly, it serves as the foundation from which we can reference and structure input logic in other scripts.
Setting up a new Input Script
Create a new C# script to serve as your Player Input. This is where we’ll define and handle the Player’s actions.
To use the new Input System in this script, start by adding its namespace at the top.
Next, create a reference to the generated Input class (its name should match the one you just generated — autocomplete will help if enabled).
Now, we can enable input by calling the Enable
method. This activates the input system for your object (in this example, a Mech):
Performing and Canceling Actions
Performing
With input enabled, you can now respond to any action defined in the Input Actions asset. In this example, we’ll handle a Fire action for my Mech object.
Note: The way performed actions are structured closely ties into delegates and events. Understanding those concepts will give you much greater control over the Input System.
Once you subscribe to a performed action, Unity will automatically generate a corresponding method.
Any logic placed inside this method will run when the input is triggered.
Here’s an example using a console message. I’ve renamed the parameter to context
, as it’s commonly used by developers and improves readability:
Concatenating the message with the context
variable allows you to see detailed input information in the console.
Note: Performed actions typically show a value of 1, which indicates the action is active — though this detail isn’t crucial for now.
Cancelling Actions
Canceling actions are similar to GetKeyUp
from the old Input System. They follow the same structure as performed actions—you just change the event to canceled.
Note: Do not copy and paste your performed method! Unity won’t generate a cancel method automatically unless you manually define the event first.
Once the cancel method is generated, you can test it just like the performed one.
While holding down the input key, only the performed action is triggered. Once you release it, the cancel action will follow.
You’ll be able to observe both inputs in the console:
I hope this article was helpful! Check out my portfolio! Looking to get hired in the video game industry! :)