- Home /
Problem is not reproducible or outdated
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?
$$anonymous$$aybe I should of clarified this a little bit more. I need to use the UnityEditor.EditorUtility.InstanceIDToObject()
in the game itself, and not the editor. If I used your suggestion, it would probably compile, but my game would not work.
I have created an object pool / cache that uses the GameObject.GetInstanceID()
function to store the int values into and array. this proved to allow caching of 10x the amount of object IDs vs. the GameObjects themselves with the same performance. The silly thing is, when I actually want to look into that array and reference a GameObject, the only way I have found to do so is UnityEditor.EditorUtility.InstanceIDToObject()
. You think there would be a function withing the UnityEngine to do the same.
So we can either find a way to take an ObjectID(int) and cast it to a GameObject, or find a way to have the UnityEditor.dll included in the build...
I'm still pretty noob. Thanks = )
Answer by Landern · Sep 04, 2014 at 03:38 PM
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
This does NOT work!!! Because the InstanceIDToObject()
need to take a parameter: int instanceID
!!!
Answer by GreekStudios0 · Sep 04, 2014 at 03:43 PM
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
Answer by ForbiddenSoul · Sep 04, 2014 at 05:59 PM
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?
I don't think is possible to use UnityEditor reference outside the Unity Editor.
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Multiple Cars not working 1 Answer
Movement Script Help 1 Answer
C# Plane Detecting a Gameobject 1 Answer