S
S
SkyCrusher2020-07-20 20:45:53
Unity
SkyCrusher, 2020-07-20 20:45:53

Why doesn't Unity attach a gameobject in the custom inspector field?

In the class file for which the editor script is written, a two-dimensional array of gameobjects is declared:
public GameObject[][] blocks;
The script for the editor itself

using GameLayer.GameManagers;
using UnityEditor;
using UnityEngine;

namespace Editor
{
    [CustomEditor(typeof(LevelGameManager))]
    public class LevelGameManagerEditor : UnityEditor.Editor
    {
        private int rows, columns;
        
        public override void OnInspectorGUI()
        {
            DrawDefaultInspector();
            
            EditorGUILayout.Space(30);
            
            LevelGameManager myTarget = target as LevelGameManager;

            EditorGUILayout.LabelField("Blocks");
            rows = EditorGUILayout.IntField("Rows count", rows);
            columns = EditorGUILayout.IntField("Columns count", columns);
            
            myTarget.blocks = new GameObject[rows][];
            
            for (int i = 0; i < rows; i++)
            {
                EditorGUILayout.LabelField($"Row {i + 1}");
                myTarget.blocks[i] = new GameObject[columns];
                
                for(int j = 0; j < columns; j++)
                {
                    myTarget.blocks[i][j] = (GameObject) EditorGUILayout.ObjectField($"Column {j + 1}", myTarget.blocks[i][j], typeof(GameObject), true);
                }
            }
        }
    }
}

The problem is that Unity, although it displays a list of project gameobjects, does not want to attach and "remember" that you dragged something into it.
5f15d83e2674b060777159.png
How to fix it?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question