Help with custom EditorWindow
Hi I have i problem that has been bugging me for 2 days now i can't solve. So i have a custom class with 2 variables, and I want through custom item menu(class that inherits the EditorWindow class) to add elements to an array of this custom class, but i always get a NullReferenceException: Object reference not set to an instance of an object . If it is not an array, but a single variable of this custom class it works, but the moment i make it an array it gives this array. I make sure to initialize the array with new so this is not the problem.
Please share your code, its impossible to know whats wrong without seeing the code you are trying to run.
Answer by Statement · Oct 31, 2015 at 12:38 PM
Hi I have i problem that has been bugging me for 2 days now i can't solve. So i have a custom class with 2 variables.
class CustomClass
{
object var1;
object var2;
}
and I want through custom item menu(class that inherits the EditorWindow class) to add elements to an array of this custom class,
class CustomItemMenu : EditorWindow
{
CustomClass[] array = { }; // empty array of CustomClass
[MenuItem("Window/Array test")]
static void ShowWindow()
{
GetWindow<CustomItemMenu>().Show();
}
void OnGUI()
{
GUILayout.Label("array.Length: " + array.Length);
if (GUILayout.Button("Add"))
{
// add element to array
CustomClass[] newArray = new CustomClass[array.Length + 1];
for (int i = 0; i < array.Length; ++i)
newArray[i] = array[i];
newArray[array.Length] = new CustomClass();
array = newArray;
}
}
}
but i always get a NullReferenceException: Object reference not set to an instance of an object. If it is not an array, but a single variable of this custom class it works, but the moment i make it an array it gives this array. I make sure to initialize the array with new so this is not the problem.
Dunno, works for me. However, CustomClass is not serializable so the array will lose contents on script reload.
What exactly is null? The array? The objects in the array? The fields on the objects in the array?
If your custom class have a field of string type, Unity may automatically set null string to empty string when it loads your EditorWindow. However, when you create the object yourself, you are responsible of making sure all fields of CustomClass is set properly.
That is the problem - I am not sure what is null. So i have this code: for(int i = 0; i < array.Length; i++){ /ex/ customClassArray[i].firstValue = EditorGUILayout.FloatField("First Value", customClassArray[i].firstValue); } and i get the Exception at line "ex", but if I do it like this for(int i = 0; i < array.Length; i++) { /ex/ customClassVar.firstValue = EditorGUILayout.FloatField("First Value", customClassVar.firstValue); } (not an array but just a single variable) it works.(also sorry i can't get the code to show properly)What exactly is null? The array? The objects in the array? The fields on the objects in the array?>
I am not sure what is null.
Figure out what is null!
Check the error message. Double click the message and it'll highlight the line of code where the exception was thrown. If you have daisychained access like foo.bar.baz.dog = 3 then split that sausage up and test foo, foo.bar, foo.bar.baz individually..
Or hook up a debugger (monodevelop tutorial).
It looks as if you meant to include some code, but there is none.
Okay.
The exception is thrown executing this line:
customClassArray[i].firstValue = EditorGUILayout.FloatField("First Field", customClassArray[i].firstValue);
We know the array is not null. Why? Because you could access .Length
in the for loop. customClassArray[i]
could return null. Nothing else can throw a null reference exception from that line of code. So it means, your customClassArray contain null elements. Wherever you create your array, you need to populate the items in the array to something meaningful, or provide a default behaviour for null objects.
var item = customClassArray[i];
if (item == null)
{
GUILayout.Label("(null)");
}
else
{
item.firstValue = EditorGUILayout.FloatField("First Field", item.firstValue);
}
Your answer

Follow this Question
Related Questions
DrawGizmo - Accessing Editor script in DrawGizmo 1 Answer
Unity Custom Editor - Lauch callback at the end of the SerializeField. 0 Answers
Serialize child ScriptableObject asset values in parent ScriptableObject asset. 0 Answers
Unity 2019 LTS can't find scripts in "Editor" Folder. 0 Answers
Custom unity editor resetting values 0 Answers