How do I disable and enable the input system?

Hello All,

I have the player input moving, he runs and jumps and does all sorts of things, however, I’d like to disable it and enable it at will, for example when I need to prevent player input while a big boss appears and shows off for a few seconds.

I’m looking at this page on the unity docs which says to use PlayerInput.DeactivateInput. However I can’t find a way to use this.

Is it a case of using:
PlayerInput input = GetComponent()
then calling
input.DeactivateInput()?
The site doesn’t say.
The reason I can’t try this at the moment is because my player input script is also called PlayerInput, and I don’t know how to differentiate between the player input class I added to the gameobject, and the Unity PlayerInput that’s added to the class to use the new input system.

Thanks in advance.

Hi,
you probably want to deactivate the actionmap that is used for the player movement, i.e. by calling

input.YourActionMap.Disable();
1 Like

Please refer to my comment further below as I made a big mistake years ago when making this comment. I feel like it’s caused a lot of google searches to come up with issue.

1 Like

Thank you. I’ll give that a try.

Does this code work if you are using the Legacy input system?

Uh no. It’s for the new input system. It’s not relevant at all to the old system.

2 Likes

Ok thanks. Any idea how to do it with the old system?

You can’t disable the old input system as far as I’m aware. You can only introduce your own system to guard any code that reads input from executing.

1 Like

Ya I was thinking of just making another bool around any input code, is that what you mean?

Yes, that is what I mean.

Though really I would use a guard-pattern:

private void Update()
{
    if ([GameIsPaused] == true)
    {
        return;
    }
   
    // input reading code
}
1 Like

I’ll have to research this, I’ve never used this pattern before! Thank you.

Hmm, doesn’t seem to work…
It doesn’t have methods Enable() and Disable(), but does have ActivateInput() and DeactivateInput().

I used DeactivateInput(), but it still returns a warning at PlayerInput input = new PlayerInput();:

I very much shorthanded the code, but I believe I made the mistake of saying you can declare a new PlayerInput component when I was trying to convey you can disable the inputactionmap. I was relatively new to the system and completely missed the PlayerInput reference I made.

In truth, it would be more along the lines of (more quick code, haven’t checked it) -

private YourActionMap _actionMap;

void Awake(){
_actionMap = new YourActionMap();
}

void OnEnable(){
_actionMap.Jump.performed += JumpAciton;
_actionMap.Enable();
}

void OnDisable(){
_actionMap.Jump.performed -= JumpAciton;
_actionMap.Disable();
}

This is using the script generated from the input asset.

As for the original purpose of the post, here’s a testing script I used to show how to disable or enable a player input-

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class DEBUGPlayerInput : MonoBehaviour
{
    public PlayerInput _playerInput;

    IEnumerator Start()
    {
        while (true)
        {
            yield return new WaitForSeconds(1f);
            _playerInput.DeactivateInput();
            Debug.Log("Deactivated Input");
            yield return new WaitForSeconds(1f);
            _playerInput.ActivateInput();
            Debug.Log("Activated Input");
        }
    }
}

You were correct in that you need to call DeactivateInput or ActivateInput, you just need to reference the PlayerInput.

I apologize for the general confusion my previous comment probably caused, I didn’t know there were previous comments asking about the code I posted. I’ll change it up a bit to reflect this comment.