Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

NullReferenceException at InputActionMap.ResolveBindings() in 0.2.1

Discussion in 'Input System' started by Nixaan, Apr 8, 2019.

  1. Nixaan

    Nixaan

    Joined:
    May 30, 2013
    Posts:
    118
    I get the exception by simply changing how the .inputactions asset is referenced (the asset generated class is named InputSystemMaps). Unity 2018.3.9, input system 0.2.1

    Next are the 2 variants of the code that reference the asset (the difference is swapping lines 13 and 14, the 2 lines in the Awake method).
    In Variant 1 (using line 13) all works fine.
    But in Variant 2 (using line 14) an exception is thrown OnEnable() line 20 (full exception after the code).

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Experimental.Input;
    3.  
    4. public class InputSystemTest : MonoBehaviour
    5. {
    6.     [SerializeField] private InputSystemMaps ism = default;
    7.     [SerializeField] private SimpleSo controlsSimple = default;
    8.  
    9.     private InputSystemMaps inputSystemMapsRef = default;
    10.  
    11.     private void Awake()
    12.     {
    13.         // inputSystemMapsRef = ism; // Variant 1: serialized field in the MonoBehaviour
    14.         inputSystemMapsRef = controlsSimple.inputSystemMaps;  // Variant 2: serialized field in ScriptableObject
    15.     }
    16.  
    17.     public void OnEnable()
    18.     {
    19.         if (!inputSystemMapsRef.PlayerCharacter.enabled)
    20.             inputSystemMapsRef.PlayerCharacter.Enable();
    21.     }
    22.  
    23.     public void OnDisable()
    24.     {
    25.         if (inputSystemMapsRef.PlayerCharacter.enabled)
    26.             inputSystemMapsRef.PlayerCharacter.Disable();
    27.     }
    28. }
    29.  

    Next is the full text of the exception:

    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. UnityEngine.Experimental.Input.InputActionMap.ResolveBindings () (at Library/PackageCache/com.unity.inputsystem@0.2.1-preview/InputSystem/Actions/InputActionMap.cs:854)
    3. UnityEngine.Experimental.Input.InputActionMap.ResolveBindingsIfNecessary () (at Library/PackageCache/com.unity.inputsystem@0.2.1-preview/InputSystem/Actions/InputActionMap.cs:746)
    4. UnityEngine.Experimental.Input.InputActionMap.Enable () (at Library/PackageCache/com.unity.inputsystem@0.2.1-preview/InputSystem/Actions/InputActionMap.cs:305)
    5. InputSystemMaps+PlayerCharacterActions.Enable () (at Assets/AssetsForLevels/SharedCode/ControlsManager/InputSystem/InputSystemMaps.cs:98)
    6. InputSystemTest.OnEnable () (at Assets/InputSystemTest.cs:20)

    Just in case next is the scriptable object:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.     [CreateAssetMenu(menuName = "Controls/SimpleSo", fileName = "SimpleSo")]
    6.     public class SimpleSo : ScriptableObject
    7.     {
    8.         [SerializeField] public InputSystemMaps inputSystemMaps;
    9.     }
    10.  
     
    Last edited: Apr 8, 2019