解决InputSystem读取报错UnityException: CreateScriptableObjectInstanceFromType is not allowed…

报错内容:UnityException: CreateScriptableObjectInstanceFromType is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour

报错内容提示我们,ScriptableObject实例不能脱离Unity生命周期创建,不允许在变量定义时直接初始化。

解决方案:

在Unity生命周期中的Awake或Start来初始化变量。

代码:

    CharacterInputActions  _InputActionCtrl;
    public Vector2 MovementValue{get{return _InputActionCtrl.Default.Movement.ReadValue<Vector2>();}}
    private void Awake() {
        if (_InputActionCtrl==null)
        {
            _InputActionCtrl=new CharacterInputActions();
        }
    }
    private void OnEnable() {
        _InputActionCtrl.Enable();
    }
    private void OnDisable() {
        _InputActionCtrl.Disable();
        
    }

 

作者:Miracle
来源:麦瑞克博客
链接:https://www.playcreator.cn/archives/unity/unity_technologyshare/2419/
本博客所有文章除特别声明外,均采用CC BY-NC-SA 4.0许可协议,转载请注明!
THE END
分享
海报
解决InputSystem读取报错UnityException: CreateScriptableObjectInstanceFromType is not allowed…
报错内容:UnityException: CreateScriptableObjectInstanceFromType is not allowed to be called from a MonoBehaviour constructor (or instance field ini……
<<上一篇
下一篇>>
文章目录
关闭
目 录