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
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.
官方文档
https://docs.unity3d.com/ScriptReference/ScriptableObject.CreateInstance.html解决方案
-
Replace 'new MyScriptableObject()' with 'ScriptableObject.CreateInstance<MyScriptableObject>()'.
-
If you need to initialize fields, create a static factory method that calls CreateInstance and then sets properties.
无效尝试
常见但无效的做法:
-
95% 失败
ScriptableObject must be created via CreateInstance, not by new, regardless of serialization attributes.
-
85% 失败
ScriptableObject requires explicit initialization via CreateInstance to set up internal Unity engine state.