Hi, I am using CustomPropertyDrawer to create 2D Array I can manipulate in the inspector, but I keep getting this error (sorry in advance for the length of the error.
Editor Script.
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(CustomUnitType))]
public class CustomTileData : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.PrefixLabel(position, label);
Rect newPosition = position;
newPosition.y += 20f;
SerializedProperty rows = property.FindPropertyRelative("rows");
int LengthOfArray = 7;
for (int i = 0; i < 8; i++)
{
SerializedProperty row = rows.GetArrayElementAtIndex(i).FindPropertyRelative("row");
newPosition.height = 20;
if (row.arraySize != LengthOfArray)
{
row.arraySize = LengthOfArray;
}
newPosition.width = 20;
for (int j = 0; j < LengthOfArray; j++)
{
//Debug.Log("ED: j " + j);
EditorGUI.PropertyField(newPosition, row.GetArrayElementAtIndex(j), GUIContent.none);
newPosition.x += newPosition.width;
}
newPosition.x = position.x;
newPosition.y += 20;
}
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return 20 * 9;
}
}
Type, above script is referencing.
using UnityEngine;
using System.Collections;
[System.Serializable]
public class CustomUnitType
{
[System.Serializable]
public struct rowData
{
public int[] row;
}
public rowData[] rows = new rowData[7];
}
Your for loop is doing 8 iterations while the rows array length is only 7. The rows.GetArrayElementAtIndex(i) will return null for the out of bounds element.