- Home /
Writing Editor Script, Getting Weird Null Reference Exception
I am writing a custom editor that will allow the user to add items to a list and print labels in the inspector for each item.
I declare the list as follows:
public List<string> StageTable;
A bit later in the code I have this piece of code to create a label for each string in StageTable.
foreach(string st in StageTable)
{
EditorGUILayout.LabelField("test string");
}
I can see the result in the inspector, but when I try the game it blanks out the inspector where the result should be showing nothing there, and throws this long exception:
NullReferenceException:
Object reference not set to an instance of an object
APG_CustomEditor.OnInspectorGUI
() (at Assets/Editor/APG_CustomEditor.cs:53)
UnityEditor.InspectorWindow.DrawEditors
(Boolean isRepaintEvent, UnityEditor.Editor[]
editors, Boolean eyeDropperDirty)
(at C:/BuildAgent/work/14194e8ce88cdf47/Editor/Mono/ ...
Inspector/InspectorWindow.cs:888)
UnityEditor.InspectorWindow.OnGUI ()
(at C:/BuildAgent/work/14194e8ce88cdf47/Editor/Mono/ ...
Inspector/InspectorWindow.cs:243)
System.Reflection.MonoMethod.Invoke (System.Object obj,
BindingFlags invokeAttr, System.Reflection.Binder binder,
System.Object[] parameters,
System.Globalization.CultureInfo culture)
Any help or input on this matter is of course, most welcome!
Isnt declaring it with:
public List StageTable;
And then adding to it with:
StageTable.Add("stage");
Enough? I might be totally wrong here but it seemed to work when I created lists before.
new List();
spaces to trick Answers into not treating the like a html tag.
Answer by Chronos-L · Apr 10, 2013 at 06:17 AM
I think that the StageTable
is not initialized when you try to built the LabelField
for it. Null list and blank list is different, the first will produce nullref-exception and the second will be okay (just that there is no labelfield to be shown)
Basically I add to the list with this button:
if(GUILayout.Button("Add", GUILayout.Width (65), GUILayout.Height (20))) { StageTable.Add("stage"); }
Before doing the foreach I have an if statement to test that the list isnt empty:
if(StageTable.Count > 0)
The full code is basically this:
using UnityEditor; using UnityEngine; using System.Collections.Generic; ///* [CustomEditor(typeof(AdaptableProceduralGenerator))] // ^ This is the script we are making a custom editor for. public class APG_CustomEditor : Editor {
public List StageTable;
private bool remConfirm = false;
public override void OnInspectorGUI () {
if(StageTable.Count > 0)
{
foreach(string st in StageTable)
{
EditorGUILayout.BeginHorizontal();
bool showBtn = new bool();
EditorGUILayout.Toggle(showBtn, GUILayout.Width (10), GUILayout.Height (20));
EditorGUILayout.LabelField("Stage:",GUILayout.Width (42), GUILayout.Height (20));
EditorGUILayout.EnumPopup(test.uponReachingEndState,GUILayout.Width (100), GUILayout.Height (20));
EditorGUILayout.TextField("",GUILayout.Width (55), GUILayout.Height (20));
EditorGUILayout.TextField("",GUILayout.Width (55), GUILayout.Height (20));
bool showBtn2 = new bool();
EditorGUILayout.Toggle(showBtn2, GUILayout.Width (10), GUILayout.Height (20));
EditorGUILayout.EndHorizontal();
}
}
if(GUILayout.Button("Add", GUILayout.Width (65), GUILayout.Height (20)))
{
StageTable.Add("stage");
}
Hopefully this image will help explain whats hapenning:
It says "NullReferenceException: Object reference not set to an instance of an object APG_CustomEditor.OnInspectorGUI () (at Assets/Editor/APG_CustomEditor.cs:53)", but I can't see the line 53 in your code, and please highlight and Ctrl-$$anonymous$$ to format your code.
It just mean that StageTable
is null, for whatever reason there is out there.
For some reason my other comment doesnt show up so I'll post it here: But if I initialize it from within the script it will constanly create a new list and I wont be able to add to it, which is what is going on now after I added:
List StageTable = new List();
Also if I add it after class declaration, I dont get null references and I can add to the list, but when I click play it empties the list.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Custom list-item name 0 Answers
EditorGUI add SortingLayer-like list to custom Editor 1 Answer
Serialising subclass variables in inspector 2 Answers
Custom GUI Elements in Inspector 0 Answers