InputSystem icon indicating copy to clipboard operation
InputSystem copied to clipboard

InputActionAssetReference stored in ScriptableObject works only once

Open RRickkert opened this issue 7 years ago • 2 comments

This issue might have something to do with the issue of #353, however I wanted to explain this issue specifically with clear code to reproduce this issue.

The issue:

  • When a reference to an InputActionAssetReference instance is stored in a MonoBehaviour, everything works as expected.
  • When a reference to an InputActionAssetReference instance is stored in a ScriptableObject, everything works only the first time starting the game after creating a ScriptableObject in the Project folder. Stopping the game and starting it again, no events are called.

The following code samples contain a class called "InputMaster" which is the name of the generated class by the Input Manager.

Code sample storing the reference in a MonoBehaviour, which works:

The AMonoBehaviourClass must be added to a GameObject, and the "Controls" variable must be set to the "InputMaster" instance which is created by the Input Manager in your Project Folder.

AMonoBehaviourClass.cs:

using UnityEngine;

public class AMonoBehaviourClass : MonoBehaviour
{
    [SerializeField] private InputMaster _controls;

    private void Awake()
    {
        _controls.Enable();
        _controls.Player.Shoot.performed += (e) => Debug.Log("Fired");
    }
}

Code sample storing the reference in a ScriptableObject, which only works the first time:

In this case, after creating these classes, you first create an instance of the ScriptableObject in the Project Folder, by using Create --> ScriptableObjects --> AScriptableObjectClass. The created instance must have the variable "Controls" set to the "InputMaster" instance which is created by the Input Manager in your Project Folder. The ASecondMonoBehaviourClass must be added to a GameObject, and the "AScriptableObjectInstance" reference must be set to the created instance in your Project Folder.

ASecondMonoBehaviourClass.cs:

using UnityEngine;

public class ASecondMonoBehaviourClass : MonoBehaviour
{
    [SerializeField] private AScriptableObjectClass _aScriptableObjectInstance;

    private void Awake()
    {
        _aScriptableObjectInstance.Controls.Enable();
        _aScriptableObjectInstance.Controls.Player.Shoot.performed += (e) => Debug.Log("Fired");
    }
}

AScriptableObjectClass.cs:

using UnityEngine;

[CreateAssetMenu(fileName = nameof(AScriptableObjectClass), menuName = "ScriptableObjects/"+nameof(AScriptableObjectClass))]
public class AScriptableObjectClass : ScriptableObject
{
    [SerializeField] private InputMaster _controls;

    public InputMaster Controls => _controls;
}

RRickkert avatar Dec 28 '18 16:12 RRickkert

Thank you for the detailed repro. We'll take a look.

Rene-Damm avatar Jan 15 '19 17:01 Rene-Damm

Verified fix on 2019.1.0b9 with Develop (af5d4c1)

JiaUnity avatar Mar 28 '19 23:03 JiaUnity