I am trying to learn Unity’s Input-System. I am working on a character controller which can switch from first to third person using Cinemachine. I created a PlayerControls.inputactions asset which generated a PlayerControls.cs file. I have a InputManager script with a public PlayerControls variable. There are no shown errors, but when I attach it to a game object in the scene, the InputManager script is blank on the Inspector.
Can someone help me figure this out?
I have tried reinstalling unity, the input system package, etc.
InputManager.cs
using UnityEngine;
using UnityEngine.InputSystem;
public class InputManager : MonoBehaviour
{
public PlayerControls controls; // Directly reference the PlayerControls asset
void OnEnable()
{
// Use the controls directly without loading
controls.Gameplay.Enable();
}
void OnDisable()
{
controls.Gameplay.Disable();
}
// Implement callback methods for your actions here
}
PlayerControls.cs
//------------------------------------------------------------------------------
// <auto-generated>
// This code was auto-generated by com.unity.inputsystem:InputActionCodeGenerator
// version 1.7.0
// from Assets/Scripts/PlayerControls.inputactions
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Utilities;
public partial class @PlayerControls: IInputActionCollection2, IDisposable
{
public InputActionAsset asset { get; }
public @PlayerControls()
{
asset = InputActionAsset.FromJson(@"{
""name"": ""PlayerControls"",
""maps"": [
{
""name"": ""Gameplay"",
""id"": ""3dc1c7e0-a6c6-473b-8d8b-1aa58ef67006"",
""actions"": [
{
""name"": ""Move"",
""type"": ""Value"",
""id"": ""d13880ed-0142-468b-867f-a2a1fc8cfb6f"",
""expectedControlType"": ""Vector2"",
""processors"": """",
""interactions"": """",
""initialStateCheck"": true
},
{
""name"": ""TogglePerspective"",
""type"": ""Button"",
""id"": ""67d8fe40-cbab-42c7-a78a-c46ddf064962"",
""expectedControlType"": ""Button"",
""processors"": """",
""interactions"": """",
""initialStateCheck"": false
}
],
""bindings"": [
{
""name"": ""2D Vector"",
""id"": ""cab8e8fd-f350-4800-9e0e-b4b8766d31c2"",
""path"": ""2DVector"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Move"",
""isComposite"": true,
""isPartOfComposite"": false
},
{
""name"": ""up"",
""id"": ""95ce44c4-fa27-4e53-8e86-9782ca298d64"",
""path"": ""<Keyboard>/w"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Move"",
""isComposite"": false,
""isPartOfComposite"": true
},
{
""name"": ""down"",
""id"": ""b3b3368a-aaa3-48fd-9eeb-c24fa3411cf6"",
""path"": ""<Keyboard>/s"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Move"",
""isComposite"": false,
""isPartOfComposite"": true
},
{
""name"": ""left"",
""id"": ""480db361-b069-4bcc-9c72-9eb37472e302"",
""path"": ""<Keyboard>/a"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Move"",
""isComposite"": false,
""isPartOfComposite"": true
},
{
""name"": ""right"",
""id"": ""09e1bace-a124-44c5-bba4-82881c2f4f47"",
""path"": ""<Keyboard>/d"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Move"",
""isComposite"": false,
""isPartOfComposite"": true
},
{
""name"": """",
""id"": ""825fb12f-f44f-4a6b-aed8-e3238b06c787"",
""path"": ""<Keyboard>/t"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""TogglePerspective"",
""isComposite"": false,
""isPartOfComposite"": false
}
]
}
],
""controlSchemes"": []
}");
// Gameplay
m_Gameplay = asset.FindActionMap("Gameplay", throwIfNotFound: true);
m_Gameplay_Move = m_Gameplay.FindAction("Move", throwIfNotFound: true);
m_Gameplay_TogglePerspective = m_Gameplay.FindAction("TogglePerspective", throwIfNotFound: true);
}
public void Dispose()
{
UnityEngine.Object.Destroy(asset);
}
public InputBinding? bindingMask
{
get => asset.bindingMask;
set => asset.bindingMask = value;
}
public ReadOnlyArray<InputDevice>? devices
{
get => asset.devices;
set => asset.devices = value;
}
public ReadOnlyArray<InputControlScheme> controlSchemes => asset.controlSchemes;
public bool Contains(InputAction action)
{
return asset.Contains(action);
}
public IEnumerator<InputAction> GetEnumerator()
{
return asset.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Enable()
{
asset.Enable();
}
public void Disable()
{
asset.Disable();
}
public IEnumerable<InputBinding> bindings => asset.bindings;
public InputAction FindAction(string actionNameOrId, bool throwIfNotFound = false)
{
return asset.FindAction(actionNameOrId, throwIfNotFound);
}
public int FindBinding(InputBinding bindingMask, out InputAction action)
{
return asset.FindBinding(bindingMask, out action);
}
// Gameplay
private readonly InputActionMap m_Gameplay;
private List<IGameplayActions> m_GameplayActionsCallbackInterfaces = new List<IGameplayActions>();
private readonly InputAction m_Gameplay_Move;
private readonly InputAction m_Gameplay_TogglePerspective;
public struct GameplayActions
{
private @PlayerControls m_Wrapper;
public GameplayActions(@PlayerControls wrapper) { m_Wrapper = wrapper; }
public InputAction @Move => m_Wrapper.m_Gameplay_Move;
public InputAction @TogglePerspective => m_Wrapper.m_Gameplay_TogglePerspective;
public InputActionMap Get() { return m_Wrapper.m_Gameplay; }
public void Enable() { Get().Enable(); }
public void Disable() { Get().Disable(); }
public bool enabled => Get().enabled;
public static implicit operator InputActionMap(GameplayActions set) { return set.Get(); }
public void AddCallbacks(IGameplayActions instance)
{
if (instance == null || m_Wrapper.m_GameplayActionsCallbackInterfaces.Contains(instance)) return;
m_Wrapper.m_GameplayActionsCallbackInterfaces.Add(instance);
@Move.started += instance.OnMove;
@Move.performed += instance.OnMove;
@Move.canceled += instance.OnMove;
@TogglePerspective.started += instance.OnTogglePerspective;
@TogglePerspective.performed += instance.OnTogglePerspective;
@TogglePerspective.canceled += instance.OnTogglePerspective;
}
private void UnregisterCallbacks(IGameplayActions instance)
{
@Move.started -= instance.OnMove;
@Move.performed -= instance.OnMove;
@Move.canceled -= instance.OnMove;
@TogglePerspective.started -= instance.OnTogglePerspective;
@TogglePerspective.performed -= instance.OnTogglePerspective;
@TogglePerspective.canceled -= instance.OnTogglePerspective;
}
public void RemoveCallbacks(IGameplayActions instance)
{
if (m_Wrapper.m_GameplayActionsCallbackInterfaces.Remove(instance))
UnregisterCallbacks(instance);
}
public void SetCallbacks(IGameplayActions instance)
{
foreach (var item in m_Wrapper.m_GameplayActionsCallbackInterfaces)
UnregisterCallbacks(item);
m_Wrapper.m_GameplayActionsCallbackInterfaces.Clear();
AddCallbacks(instance);
}
}
public GameplayActions @Gameplay => new GameplayActions(this);
public interface IGameplayActions
{
void OnMove(InputAction.CallbackContext context);
void OnTogglePerspective(InputAction.CallbackContext context);
}
}
I am using #Unity-2022-3-LTS.20f1 and Input-System 1.7.0

Glad to help. The official documentation is always a good place to start.
– dstears