It seems like editor gui scripts are very unstable, constantly failing etc. (atleast for me)
I’m getting this error:
NullReferenceException: Object reference not set to an instance of an object
UnityEditor.InspectorWindow.DrawEditors (Boolean isRepaintEvent, UnityEditor.Editor[] editors, Boolean eyeDropperDirty) (at C:/BuildAgent/work/d3d49558e4d408f4/Editor/Mono/Inspector/InspectorWindow.cs:850)
UnityEditor.DockArea:OnGUI()
with this code:
using UnityEngine;
using System.Collections;
using System;
using UnityEditor;
[CustomEditor(typeof(myScript))]
public class myScriptEditor : Editor
{
SerializedObject eTarget;
void OnEnable()
{
// Load some gui content here (textures for toolbar etc.)
// the SerializedObject
eTarget = new SerializedObject(target);
}
public override void OnInspectorGUI()
{
eTarget.Update(); // <- error here
}
}
I’m also drawing some textures in “OnInspectorGUI()” like this
GUIStyle style = new GUIStyle();
style.normal.background = EditorGUIUtility.Load("logo.png") as Texture2D;
GUILayout.Label(GUIContent.none, style);
So what might cause this error?
It’s popoing up randomly, sometimes it doesnt apear, but sometimes it’s the only thing I see…
Anytime you get null reference errors, you can fix them (or at least make the error go away) by checking if a variable is null (or ensuring that it’s not null) before you ask it to do whatever you’re asking it. In this case, you’re asking eTarget to update when you don’t know that eTarget has any value.
public override void OnInspectorGUI()
{
if (eTarget == null) eTarget = new SerializedObject(target);
eTarget.Update(); // <- no more error here :)
}
In this case, that’s treating a symptom of a second problem: you’re expecting OnEnable to do something in an Editor script. OnEnable is a part of MonoBehaviour, not of Editor, and that function will never be getting called.
Oh, OnEnable is waaaay down at the bottom of the page. Also must be kind of new-ish, it didn’t have that when I started writing inspectors…
If you’re still getting errors, then track them down and apply the same kind of logic to those lines as well. Null reference errors simply mean that something is null and you’re digging into them. So to avoid them you just need to
Why are you using your own eTarget variable instead of using the provided (and presumably never null) serializedObject property, as they use in that example?
Wel lI understand the null reference errors. But what I dont understand is, that why this all is so unstable…
basically I have to check everything for being null or not.
Maybe I’m initializing gui contets in the wrong way?
This is how I do it:
using UnityEngine;
using System.Collections;
using System;
using UnityEditor;
[CustomEditor(typeof(myScript))]
public class myScriptEditor : Editor
{
private int selectionId;
private int xCount = 4;
private GUIContent[] guiContent = new GUIContent[8]; // correct?
void OnEnable()
{
// some params taken in...
}
public override void OnInspectorGUI()
{
SerializedObject.Update();
carGuiContent[0].tooltip = "toolbar"; // <- error here
carGuiContent[0].image = EditorGUIUtility.Load("icon.png") as Texture2D;
// continue filling GUIContent array...
selectionId = GUILayout.SelectionGrid(selectionId,guiContent,xCount);
serializedObject.ApplyModifiedProperties();
}
}
I changed my eTarget to serializedObject as sugested.
Use the lower case s - serializedObject and SerializedObject are very different You need the serializedObject member variable of the Editor, not the SerializedObject class itself. I am guessing that that line as typed there would give you a compile error (something like “Update is not a member of class SerializedObject”).
Where do you declare and initialize carGuiContent? (Since this is a different name than what you have on line 12.) If it’s not declared, that’d be another compile error. Trying to access an item in an array that hasn’t been initialized is another source of null reference errors, or index out of range errors (if the array has been initialized but has no items in it).
I know the other errors you’re getting can crowd out the compile errors, but if you hit “clear” on the console, it’ll clear all of those leaving just the compile errors. Make sure you check them before you go elsewhere with questions - your compiler knows more about what’s going on in your script than I do!
Seems like unity is having really hard times with GUIContent arrays. Loading a image for a single label works ok, but an array is too much for unity… I wonder how other people are doing theyir gui toolbars with icons?
I see in your code that you are initializing the array of guiContent, but do you also initialize the actual instances (guiContent = new GUIContent…)? That seems to be missing, meaning your array is != null, but the instances are still null.
This is funny. It actually works this way, if I re create the instances. And whats weird is that I cant use array length of a GUIContent for loops with GUIContent, So I have to go through every object manually. Like this:
guiContent[0] = new GUIContent();
guiContent[1] = new GUIContent();
guiContent[2] = new GUIContent();
guiContent[3] = new GUIContent();
guiContent[4] = new GUIContent();
guiContent[5] = new GUIContent();
guiContent[6] = new GUIContent();
guiContent[7] = new GUIContent();
This line only creates the array itself, which is basically a list of pointers (to GUIContent-objects), so after that you have a list of pointers pointing to 0. Otherwise the system had to call a constructor for each object inside the array, which cant be done because there doesnt have to be a default-constructor without parameters. This means you have to create the objects yourself. If you dont do it and try to access an array-element, the system recognizes that you try to access something at position null which throws an error because there cant be no object at this position.
Its a bit different if you have an array of some basic datatype like int or float, because there are no pointers used. So every element inside an int-array gets a defaultvalue (0).
Are you sure you didnt misspell something with the length-property? That is in no way specific to unity, but rather to csharp itself. I cant imagine how (and why) one should prevent using it… Did you get an error-message? This could clear it up.