unity runtime_error ai_generated true

InvalidOperationException: 不允许从构造函数创建 ScriptableObject

InvalidOperationException: It is not allowed to create a ScriptableObject from the constructor

ID: unity/scriptableobject-constructor-called

其他格式: JSON · Markdown 中文 · English
93%修复率
88%置信度
1证据数
2024-03-12首次发现

版本兼容性

版本状态引入弃用备注
Unity 2022.3 active
Unity 2023.1 active
Unity 2023.2 active

根因分析

在构造函数中使用了 'new' 关键字创建 ScriptableObject 实例,应使用 ScriptableObject.CreateInstance。

English

A ScriptableObject instance was created using the 'new' keyword in a constructor instead of ScriptableObject.CreateInstance.

generic

官方文档

https://docs.unity3d.com/ScriptReference/ScriptableObject.CreateInstance.html

解决方案

  1. Replace 'new MyScriptableObject()' with 'ScriptableObject.CreateInstance<MyScriptableObject>()'.
  2. If you need to initialize fields, create a static factory method that calls CreateInstance and then sets properties.

无效尝试

常见但无效的做法:

  1. 95% 失败

    ScriptableObject must be created via CreateInstance, not by new, regardless of serialization attributes.

  2. 85% 失败

    ScriptableObject requires explicit initialization via CreateInstance to set up internal Unity engine state.