How to Check if a Button is Pressed in Unity - New Input System

preview_player
Показать описание
How do you check if the button is pressed in Unity, if your project uses New Input System?

That's very simple! First of all, the New Input System doesn't have the same if statement to detect if a button is pressed, as it was the case with the old input system. Instead, you should add an input actions asset and make a set of actions that can be part of the input system. Inside, you need to add a layer and an action that will be performed when you press the button. I will choose space bar, but you can choose any other key if you want.

Now you need a script. Inside that script, you need to introduce the Input System. You also need the input action to make sure that it is the reference to that action. After defining the input system you need the OnEnable and OnDisable voids that will define a new function: Test. The test function will have a line of code that checks if you perform the action, and also a line that will send a message that will tell that it's working. Save the script and test it.

If you encounter the error NullReferenceException, this is because you haven't called the input system in void Awake, which happens immediately when the game is launched.

If you have an indie game project that you need help with, feel free to contact me by sending a DM on Instagram. I will be happy if I can work on projects that will help people learn some new skills in video game industry.

Join this channel to get access to perks:

Support the Creator of Kozmobot on the social networks!

Рекомендации по теме
Комментарии
Автор

If you have an indie game project that I need to help you with, feel free to send me a DM on Instagram!

kozmobotgames
Автор

Hi, can you make tutorial about how to make discrete swipe (with step of 1 for example) for infinite runner? On new input system as always. Many thanks!!!

drachenko
visit shbcf.ru