The name `UnityEditor' does not exist in the current context

Hi there,

In one of my C# files I call the UnityEditor.EditorUtility.InstanceIDToObject() method.
When compiling and running in the editor everything runs fine, however, when I try to build to a windows stand-alone application I am presented with the error “The name `UnityEditor’ does not exist in the current context”

If I use the Unity Editor Name space instead,

Using UnityEditor;
EditorUtility.InstanceIDToObject()

Once again, works fine playing in the editor, stand alone build fails with error:
“The type or namespace name `UnityEditor’ could not be found. Are you missing a using directive or an assembly reference?”

UnityEditor.dll has been referenced in all of my visible MonoDevelop projects by default.

How do we fix this problem?

The UnityEditor assembly isn’t packaged up with a built out project, it’s 100% for Unity’s IDE or editor.

wrap your usings and methods from UnityEngine with a compiler directive.

Example:

#if UNITY_EDITOR
using UnityEditor;
#endif
// other code, class definition blah blah
#if UNITY_EDITOR
EditorUtility.InstanceIDToObject();
#endif

etc, etc

You can either place these scripts in a folder named Editor or just add on the very top of the script #if UNITY_EDITOR and at the very bottom #end if

So like my previous comment guessed at actually using #if UNITY_EDITOR (basically telling unity to ignore the script UNLESS it is using the unity editor) breaks my game and throws more compile errors, I want to use this function during stand alone gameplay. I have tried using compiler directives to include the script and it’s dependencies like

#if UNITY_STANDALONE
#if UNITY_64
#if UNITY_4_5_1

all to no avail, any answers?