You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行时创建ScriptableObject退出后丢失数组信息求助

Hey there! Let’s tackle that NullReferenceException you’re facing with your ScriptableObject arrays in your dungeon template editor. I’ve run into this exact headache when building editor tools for SOs, so here are the most common fixes to check:

  • Your ScriptableObject’s array isn’t initialized
    This is the #1 culprit. If you’ve only declared an array like public TileType[] tileGrid; in your SO class but never assigned it a new instance, it’ll stay null—even when you create a new SO asset.
    Fix it by initializing the array in the SO’s OnEnable method (to cover new assets and reloading) or with a default value:

    [CreateAssetMenu(fileName = "DungeonTemplate", menuName = "Templates/Dungeon Template")]
    public class DungeonTemplateSO : ScriptableObject
    {
        public RoomType roomType;
        [SerializeField] private TileType[] _tileGrid;
    
        private void OnEnable()
        {
            // Initialize if null (prevents issues when creating new SOs)
            if (_tileGrid == null)
            {
                // Match this to your default grid size (e.g., 5x5 = 25 elements)
                _tileGrid = new TileType[25];
            }
        }
    }
    
  • Your editor script isn’t checking for null before accessing the array
    Even if you initialize the SO’s array, there’s a chance the target object is null (e.g., if the editor loses reference) or the array got wiped somehow. Always add a null check before modifying or drawing the array in your editor code, and use Undo.RecordObject to track changes for undo support:

    [CustomEditor(typeof(DungeonTemplateSO))]
    public class DungeonTemplateEditor : Editor
    {
        public override void OnInspectorGUI()
        {
            var template = target as DungeonTemplateSO;
            // Exit early if target is null to avoid crashes
            if (template == null) return;
    
            // Check and initialize array if needed
            if (template._tileGrid == null)
            {
                Undo.RecordObject(template, "Initialize Tile Grid");
                template._tileGrid = new TileType[25];
            }
    
            // Rest of your grid drawing logic...
        }
    }
    
  • Array size doesn’t match your grid dimensions
    If your editor lets users adjust the grid’s width/height, you need to sync the array size to match. A mismatch can lead to index out of range errors or null elements being accessed:

    // Inside your editor's grid size adjustment logic
    int gridWidth = EditorGUILayout.IntField("Grid Width", 5);
    int gridHeight = EditorGUILayout.IntField("Grid Height", 5);
    int newGridSize = gridWidth * gridHeight;
    
    if (template._tileGrid.Length != newGridSize)
    {
        Undo.RecordObject(template, "Resize Tile Grid");
        TileType[] newGrid = new TileType[newGridSize];
        // Copy existing data to avoid losing work
        System.Array.Copy(template._tileGrid, newGrid, Mathf.Min(template._tileGrid.Length, newGridSize));
        template._tileGrid = newGrid;
    }
    
  • You’re targeting the wrong ScriptableObject type in your editor
    Double-check that your [CustomEditor] attribute is pointing to the correct SO class. If it’s misspelled or targeting the wrong type, target will be null, leading to exceptions when you try to access its array:

    // Make sure this matches your SO class name exactly
    [CustomEditor(typeof(DungeonTemplateSO))]
    public class DungeonTemplateEditor : Editor { ... }
    

One quick pro tip: When modifying ScriptableObjects in the editor, always wrap changes in Undo.RecordObject—this ensures users can undo their edits, which is a must for any usable editor tool.

If you can share the full code of your ScriptableObject class and editor script, I can help pinpoint the exact line causing the issue, but these checks should cover most common cases!

内容的提问来源于stack exchange,提问作者BorrisMitM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:12:34