コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】You are trying to read Input using the UnityEngine.Input class, but you have switched active Input handling to Input System package in Player Settings.

はじめに

You are trying to read Input using the UnityEngine.Input class, 
but you have switched active Input handling to Input System package 
in Player Settings.

Input System を導入している Unity プロジェクトで
上記のエラーが発生した時の対処方法をいくつか紹介していきます

目次

スクリプトで古い Input Manager を使っている場合

using UnityEngine;

public class Example : MonoBehaviour
{
    private void Update()
    {
        if ( Input.GetKeyDown( KeyCode.Space ) ) // ★
        {
            Debug.Log( "Space" );
        }
    }
}

スクリプトで古い Input Manager の仕組み(Input クラス)を使ってしまっている場合は

using UnityEngine;
using UnityEngine.InputSystem;

public class Example : MonoBehaviour
{
    private void Update()
    {
        if ( Keyboard.current.spaceKey.wasPressedThisFrame ) // ★
        {
            Debug.Log( "Space" );
        }
    }
}

InputSystem の仕組みを使うように変更することでエラーが発生しなくなります

UI が古い Input Manager を使っている場合

f:id:baba_s:20220101210624p:plain

シーンに存在する EventSystem を選択した時に Inspector でエラーが表示されている場合は
「Replace with InputSystemUIInputModule」ボタンを押すことで

f:id:baba_s:20220101210633p:plain

エラーが発生しなくなります

古い Input Manager も併用できるようにしたい場合

f:id:baba_s:20220101211002p:plain

古い Input Manager の機能も使いたい場合は Unity メニューの
「Edit > Project Settings...」を押して

f:id:baba_s:20220101211020p:plain

左メニューで「Player」を選択して、右メニューから「Active Input Handling」を探して
選択されている項目を「Both」に変更します

f:id:baba_s:20220101211024p:plain

確認ダイアログが表示されたら「Apply」を押します
すると Unity が再起動して古い Input Manager の機能を呼び出しても
エラーが発生しなくなります