Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Keyboard is null in Unit Testing [Solved]

Discussion in 'Input System' started by ptisch, Sep 1, 2022.

  1. ptisch

    ptisch

    Joined:
    Aug 9, 2022
    Posts:
    3
    Hello people, I was trying to implement some unit tests for my Unity project during the last few days and I seem to have run into a problem ^^". Whenever I try to instantiate a Keyboard() Object, it seemingly returns null. My code at the moment looks like this (I put it in a small function which produces the same error):

    [UnityTest]
    public IEnumerator CanPressButtons()
    {
    var keyboard = new Keyboard();
    yield return keyboard;

    Press(keyboard.wKey);
    }

    and this produces the following Test error:


    System.NullReferenceException : Object reference not set to an instance of an object
    ---
    at UnityEngine.InputSystem.Keyboard.get_Item (UnityEngine.InputSystem.Key key) [0x00005] in [ProjectPath]\Library\PackageCache\com.unity.inputsystem@1.4.2\InputSystem\Devices\Keyboard.cs:1965
    at UnityEngine.InputSystem.Keyboard.get_wKey () [0x00000] in [ProjectPath]\Library\PackageCache\com.unity.inputsystem@1.4.2\InputSystem\Devices\Keyboard.cs:1274
    at CameraTests+<CanPressButtons>d__1.MoveNext () [0x00047] in [ProjectPath]\Assets\Tests\PlayModeTests\CameraTests.cs:42
    at UnityEngine.TestTools.TestEnumerator+<Execute>d__7.MoveNext () [0x0004e] in [ProjectPath]\Library\PackageCache\com.unity.test-framework@1.1.33\UnityEngine.TestRunner\NUnitExtensions\Attributes\TestEnumerator.cs:46

    I would be delighted if someone could help me fix this bug.

    Thank you all in Advance! ^^
     
  2. ptisch

    ptisch

    Joined:
    Aug 9, 2022
    Posts:
    3
    Solution: I just made a little syntactical error ^^". Insted of new Keyboard() I should have used InputSystem.AddDevice<Keyboard>().