- Home /
using UnityEditor outside of Editor folder when compiling build?
DESPERATELY need help. I have some scripts in a folder called Scripts, NOT in an Editor folder.
One of these scripts contains 'using UnityEditor'. It has a function it uses to save the state of the scene in a new scene, load another scene to play a video, then loads the temporary scene. This works fine in the Editor and everything is brilliant.
BUT when I go to compile the game in to a build, BAM, it doesn't work. I desperately need help and will be closely monitoring this question every minute until I have an answer or....something. I can submit code etc.
Main error:
"The type or namespace name `UnityEditor' could not be found. Are you missing a using directive or an assembly reference?"
I have read that to fix this error, the script in question should be moved to the Editor folder. This I do. Then other scripts fail because they don't know how to refer to the script in the Editor folder. Is there anything I can do? Please anyone, help! :(
Many thanks in advance.
Answer by sneftel · May 31, 2011 at 12:58 AM
Welll.... if you don't actually need to permanently save the game, just put it "somewhere" while some other scene is doing its thing, try this:
Have a gameobject, with a script which does the following:
Gets a list of all the gameobjects in the scene, in a class member variable.
Disable all of them EXCEPT the gameobject itself.
Load the new scene additively. This scene should have exactly ONE root object; all other objects should be its children. This root object should have a known name.
Do whatever you want to with the new scene. Afterwords...
Destroy the root object of the new scene.
Re-enable all of the objects in your saved list of gameobjects.
How do I load a scene additively? And make it work as you describe? I assume I go and use SetActiveRecursively(false) to disable the gameObjects etc.
$$anonymous$$any thanks, this set me on the right track and I've now managed to work around my issue and replace my save scene code, thank you very much. :)
I have the same problem. I need to use the funcitons EditorUtility.OpenFilePanel, EditorUtility.DisplayDialog and EditorUtility.SaveFolderPanel and it's working great in editor mode, but when I try to build it, it give me the same error (The type or namespace name `UnityEditor' could not be found. Are you missing a using directive or an assembly reference? ). Help me please, don't know what to do :_(
@fil Ask a question as a question, not as a comment
Answer by sneftel · May 31, 2011 at 12:24 AM
Damned if you do, damned if you don't. :-)
If the class is referenced (directly or indirectly) by code going into the build, but its functionality isn't actually used in the player, you can use conditional compilation to build a stub version into the player, which doesn't reference UnityEditor. Basically, like this:
#if UNITY_EDITOR
using UnityEditor;
public class Whatever {
public void foo() { EditorApplication.Beep(); }
}
#else
public class Whatever {
public void foo() { }
}
#endif
This provides a new error.
"The type or namespace name `quests' could not be found. Are you missing a using directive or an assembly reference?"
quests is the script containing the 'using UnityEditor' stuff.
$$anonymous$$ake sure that you have a stub version after the #else which compiles properly on its own.
It can't, the save function is vital, basically it saves the scene in its current state in-game, then loads a different scene which plays a video, then jumps back. Is there any alternate way to save a scene in Unity in-game?
Sure, but if it's going to be in the player, it can't be use UnityEditor functionality. There's stuff on the wiki about serializing game state.. take a look there.
Serializing game state? I'll take a look now, but it's around 2am and time is running out. Any pointers,further advice etc would be $$anonymous$$UCH appreciated.
Answer by Dune · Apr 16, 2013 at 04:48 AM
I tried a bunch of these solutions and ones very similar to this. Nothing was working until I realized I had a few empty .js files that I had saved and never added any script to. Once I deleted these blank .js files everything seemed to work. Double check any scripts you may have left blank and if you have any delete them.
omg haha you're amazing! id vote you up but i cant yet.. i had tried most solutions and nothing was working for me. i tried yours, realized i had one blank .js , deleted it, and now it works! thanks!
Answer by Kevin Barnhill · Apr 25, 2013 at 10:25 PM
Just remove the "using UnityEditor;" and you're golden.
In batch mode, if you pull in UnityEditor, but are not referencing any of its objects, Unity preprocessor doesn't know to skip that library so it throws the error.
Your answer
Follow this Question
Related Questions
UnityEngine found but MonoBehaviour or Vector3 not found in MonoDevelop 1 Answer
Is there a way to see namespaces in Editor ? (complete class name) 0 Answers
Monobehaviours & Editors with Inheritance across DLL boundaries don't load into Unity 2 Answers
Assign a MonoBehaviour script to a variable. 2 Answers
Unable to access custom editor namespace from runtime assembly 1 Answer