I am having the same error over and over after recreating this tutorial my own way.
I tried to create a custom editor for my script to spawn random objects. I keep having this error from the custom editor script when the size of my 3 arrays are over 1:
Retieving array element that was out of bounds UnityEditor.SerializedProperty:GetAwwayElementAtIndex(int32)
NullReferenceException:Object references not set to an instance of an object
UnityEditor.PropertyHandler.OnGuiLayout (UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, system.boolean includeChildren, UnityEngine.GUILayoutOption[ ] …)
Here is how I Initialize my arrays in the reference script for the editor :
Public class SpawnObjectsOnTerrain : MonoBehaviour
{
public Terrain WorldTerrain;
public LayerMask TerrainLayer;
public static float TerrainLeft, TerrainRight, TerrainTop, TerrainBottom, TerrainWidth, TerrainLength, TerrainHeight;
public static ArrayList units = new ArrayList();
public static ArrayList positions = new ArrayList();
public static ArrayList rotations = new ArrayList();
public GameObject[ ] SpawnObjects = new GameObject[QtItemToSpawn]; //in custom editor script
public int[ ] AmountOfObjects = new int[QtItemToSpawn];//in custom editor script
public float[ ] AddedHeight = new float[QtItemToSpawn];//in custom editor script
public const int QtItemToSpawn = 1;
[...]
and here is the full CustomEditorScript:
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(SpawnObjectsOnTerrain))]
public class SpawnObjectsOnTerrainEditor : Editor
{
private bool[] showObjectsSlots = new bool[SpawnObjectsOnTerrain.QtItemToSpawn];
private SerializedProperty SpawnObjectProperty;
private SerializedProperty AmountOfObjectProperty;
private SerializedProperty ObjectHeightProperty;
private const string WorldPropObjectToSpawnName = "SpawnObjects";
private const string WorldPropObjectAmountName = "AmountOfObjects";
private const string WorldPropObjectHeightName = "AddedHeight";
private void OnEnable()
{
SpawnObjectProperty = serializedObject.FindProperty(WorldPropObjectToSpawnName);
AmountOfObjectProperty = serializedObject.FindProperty(WorldPropObjectAmountName);
ObjectHeightProperty = serializedObject.FindProperty(WorldPropObjectHeightName);
}
public override void OnInspectorGUI()
{
serializedObject.Update();
for (int i=0;i < SpawnObjectsOnTerrain.QtItemToSpawn;i++)
{
SpawnObjectGUI(i);
}
serializedObject.ApplyModifiedProperties();
}
private void SpawnObjectGUI(int index)
{
EditorGUILayout.BeginVertical(GUI.skin.box);
EditorGUI.indentLevel++;
showObjectsSlots[index] = EditorGUILayout.Foldout(showObjectsSlots[index], "Object slot:" + (index.ToString()));
if (showObjectsSlots[index])
{
EditorGUILayout.PropertyField(SpawnObjectProperty.GetArrayElementAtIndex(index), new GUIContent("SetObject:" + index.ToString()));
EditorGUILayout.PropertyField(AmountOfObjectProperty.GetArrayElementAtIndex(index), new GUIContent("SetAmount:" + index.ToString()));
EditorGUILayout.PropertyField(ObjectHeightProperty.GetArrayElementAtIndex(index), new GUIContent("SetHeight:" + index.ToString()));
}
EditorGUI.indentLevel--;
EditorGUILayout.EndVertical();
}
Please Help ! It bothers me not to understand where the problem is coming from. I feel it’s the use of 3 arrays.