I have a prefab, called PieceType. It only contains a script, called PieceType.cs.
using UnityEngine;
using System.Collections;
using System;
[System.Serializable]
public class PieceType : MonoBehaviour {
public enum MovementTypes { None, Move};
public MovementTypes[,] Movement = new MovementTypes[5,5];
public string Name;
public PieceType(string n) { Name = n; }
}
With it I have an custom inspector for this script, called PieceTypeEditor.cs.
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor(typeof(PieceType))]
public class PieceTypeEditor : Editor {
public override void OnInspectorGUI()
{
PieceType myPieceType = (PieceType)target;
myPieceType.Name = EditorGUILayout.TextField("Type Name", myPieceType.Name);
for (int y = 0; y < 5; y++)
{
EditorGUILayout.BeginHorizontal ();
for (int x = 0; x < 5; x++)
myPieceType.Movement[x,y] = (PieceType.MovementTypes)EditorGUILayout.EnumPopup(myPieceType.Movement[x,y]);
EditorGUILayout.EndHorizontal ();
}
}
}
I want to edit the grid of MovementType enums for this prefab. However, I receive this error: NullReferenceException: Object reference not set to an instance of an object PieceTypeEditor.OnInspectorGUI () (at Assets/Editor/PieceTypeEditor.cs:18)
When I remove the array from Movement, that is,
public MovementTypes Movement;
(...)
myPieceType.Movement = PieceType.MovementTypes)EditorGUILayout.EnumPopup(myPieceType.Movement);
It works fine. Except for the fact its not longer an array and pretty useless. What’s wrong? I want to use these little script-prefabs as references for other objects in their scripts.
I suspect you’ve done something like not initialising the array early on in writing the script, and since adding the initialiser, you havent reset the component you are testing.
Put this in the editorscript, line 11:
if ( myPieceType.Movement == null ) myPieceType.Movement = new PieceType.MovementTypes[5, 5];
You had to figure out what exactly was giving the NullReferenceException error.
Typically you do this by checking the line number given in the error, look at the line to see what variables are on it, then using Debug.Log on a line somewhere before it to see which one is null
In your case the only reference on that line that could realisticly be null is myPieceType.Movement
Sadly true, Gotta do something like this instead
public class PieceType : MonoBehaviour {
public enum MovementTypes { None, Move };
[System.Serializable]
public class Moves {
public MovementTypes[] move;
public Moves () {
this.move = new MovementTypes[5];
}
}
public Moves[] Movement; //Note: no initialiser
}
[CustomEditor( typeof( PieceType) )]
public class newEditor : Editor {
public override void OnInspectorGUI () {
PieceType myPieceType = (PieceType) target;
if ( myPieceType.Movement == null || myPieceType.Movement[0] == null ) {
myPieceType.Movement = new PieceType.Moves[5];
return;
}
for ( int y = 0; y < 5; y++ ) {
EditorGUILayout.BeginHorizontal();
for ( int x = 0; x < 5; x++ )
myPieceType.Movement[x].move[y] = (PieceType.MovementTypes) EditorGUILayout.EnumPopup( myPieceType.Movement[x].move[y] );
EditorGUILayout.EndHorizontal();
}
}
}