EditorGUI NullReferenceException

Hello. I’m trying to use custom editors in Unity iPhone

Compiling simple custom editor script SomeEditor.cs :

using UnityEditor;
using UnityEngine;
using System;

[CustomEditor(typeof(Some))]
public class SomeEditor : Editor {
    void OnInspectorGUI () {
        GUILayoutOption[] opts = new GUILayoutOption[0];
        EditorGUILayout.IntField(1,opts);
    }

}

got error:

NullReferenceException: Object reference not set to an instance of an object
UnityEditor.EditorGUILayout.IntField (Int32 value, UnityEngine.GUILayoutOption[] options) [0x00000]  (at /Users/build/builds/unity-iphone/iphone/Editor/Mono/Generated/EditorGUI.cs:1723)

Please help me to make this work

The line

GUILayoutOption[] opts = new GUILayoutOption[0];

…creates an array with no elements. The second parameter to EditorGUILayout.IntField is optional, if you’ll excuse the pun - you can just use

EditorGUILayout.IntField(1);

…instead of passing an empty array.

Actually, even “EditorGUILayout.IntField(1);” produces the same error. In fact, just about EVERY EditorGUILayout routine produces this error.

Yep, EditorGUI doesn’t work in Unity iPhone. You’ll have to use the normal GUI instead.

Editor programming in Unity 2.1 (what Unity iPhone technically is) primarily bases on ScriptableWizard and similar.
You can’t use your 2.5 learnt / known things for Unity iPhone when it comes to editor expansion, at least at the time and the forseable future

But isn’t EditorGUILayout part of 2.1 and supposed to be supported in Unity iPhone? BTW, I ran into the exact same error message when trying to use GUILayout.SelectionGrid as well… If this is another version incompatibility issue, it’d be great if all such were documented somewhere.

Theoretically it is yes.
But it does not work exactly the same as with 2.5+

For example, you have no class Editor to extend from

Eithers it an EditorWindow or a ScriptableWizard

EditorWindow are somewhere comparable to the Editor class, but there are difference, depending on what we talk about, massive differences actually (the UI repainting for example is done through OnGUI).

I would bookmark the file file:///Applications/Unity%20iPhone/Unity%20iPhone.app/Contents/Documentation/Documentation/ScriptReference/20_class_hierarchy.Editor_Classes.html if you commonly work with Unity on the desktop so you can save yourself a lot of headache with the correct classes etc (the link should be fine, if not I’m sorry. I’ve 1.5 and 1.0.3 on my system so I renamed them with the version to prevent collisions. Important is basically only the stuff past the last / )

Yes, those are the docs I’ve been using (lately). I’ve been using Editor however on Unity iPhone 1.5 and it works fine (so long as you use GUI/GUILayout). I guess I didn’t see anywhere in the docs where it says that EditorGUILayout cannot be used in this manner.

do you have explicit code that shows where it does not work, thats otherwise correct (unlike the code in the initial posting)

Without posting my entire routine (which has grown considerably and is rather complex now), I had tried just a simple test like so:

void OnInspectorGUI()
{
   EditorGUILayout.IntField(1);
}

This raises a null reference exception.

Using GUI commands in fictive callbacks is a definitive reason it does not work.

There is no OnInspectorGUI in Unity iPhone as I already explained twice above.

There is OnGUI if you expand from EditorWindow, as mentioned too.

So before this thread goes totally out of control: please all stop assuming stuff and check out the docs if you come from Unity on the Desktop or you will be frustrated and hunt phantom issues that come from your completely wrong assumptions.

Well, that’s quite a surprise, seeing as how OnInspectorGUI() works just fine for me in Unity iPhone, so long as I use GUILayout calls. Try it yourself. It works.

The docs on using the editor features were a bit scant in 2.1/Unity iPhone, so I suppose people can be forgiven for not assuming that silence on the issue == won’t work.

There is the chance that it was an undocumented feature or something alike back then.

But in such a case, any user using it is basically on his own.
If it fails, its the users problem and there is nothing like support for it.

I personally prefer to remain in the “clear” area as I have better things to do than hunting phantom issues caused by “missbehavior” of things that are not even meant to behave basically. I guess I’m not the only one who prefer to invest the time into the project than “hacking around in the gray if there is documented functionality”

That’s totally understandable and reasonable. I always use the local docs, but I was recently referred to the online docs by someone (not realizing I was using iPhone) and I did a ton of reading, thinking it was simply updated and more fleshed out, only to realize later that none of it applied. So I’m still trying to sort stuff out in my brain as to what is supported and what isn’t. It didn’t help that my initial experiments worked (mostly) in a manner they weren’t expected to. So please be patient as I get myself sorted out again as this is my first foray into the realm of the editor.

Sure no prob :slight_smile:

For the iPhone, you can basically use most of the normal client scripting without any problem. Aside from JS type handling, the majority still applies.

For editor scripting though, I would highly recommend to go by the iPhone documentations and iphone documentations only, as you will commonly primarily find things in reference to Unity Desktop and its only rarely marked if it is <=2.1 or 2.5+

2.1 Editor Scripts work without any problems.

2.5+ though have a good chance to not work. I think 100 new functions etc have been added to the editor scripting end with 2.5 and various ones have changed.

I can’t wait 'til the 2.5 GUI subsystem is integrated with iPhone. That’ll be really sweet. There are some great new features there.