# Errors when building game to any platform

As a newer Unity user, I've never had this problem before. I looked around on the internet and didn't find any help except to reimported all my assets which did not help.

I get these errors when building to any Platform:

Assets/Scripts/MainMenuManager.cs(41,21): error CS0234: The type or namespace name EditorApplication' does not exist in the namespace UnityEditor'. Are you missing an assembly reference?

Error building Player because scripts had compiler errors

Build completed with a result of 'Failed' UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)

UnityEditor.BuildPlayerWindow+BuildMethodException: 2 errors at UnityEditor.BuildPlayerWindow+DefaultBuildMethods.BuildPlayer (BuildPlayerOptions options) [0x0021f] in C:\buildslave\unity\build\Editor\Mono\BuildPlayerWindowBuildMethods.cs:187 at UnityEditor.BuildPlayerWindow.CallBuildMethods (Boolean askForBuildLocation, BuildOptions defaultBuildOptions) [0x0007f] in C:\buildslave\unity\build\Editor\Mono\BuildPlayerWindowBuildMethods.cs:94 UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)

NullReferenceException: Object reference not set to an instance of an object UnityEditorInternal.AnimationWindowControl.GetAnimationModeDriver () (at C:/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowControl.cs:474) UnityEditorInternal.AnimationWindowControl.get_previewing () (at C:/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowControl.cs:340) UnityEditorInternal.AnimationWindowControl.get_recording () (at C:/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowControl.cs:390) UnityEditorInternal.AnimationWindowControl.StopRecording () (at C:/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowControl.cs:421) UnityEditorInternal.AnimationWindowControl.StopPreview () (at C:/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowControl.cs:361) UnityEditorInternal.AnimationWindowControl.OnDisable () (at C:/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowControl.cs:105)

NullReferenceException: Object reference not set to an instance of an object UnityEditorInternal.AnimationWindowControl.GetAnimationModeDriver () (at C:/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowControl.cs:474) UnityEditorInternal.AnimationWindowControl.get_previewing () (at C:/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowControl.cs:340) UnityEditorInternal.AnimationWindowControl.get_recording () (at C:/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowControl.cs:390) UnityEditorInternal.AnimationWindowControl.StopRecording () (at C:/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowControl.cs:421) UnityEditorInternal.AnimationWindowControl.StopPreview () (at C:/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowControl.cs:361) UnityEditorInternal.AnimationWindowControl.OnDisable () (at C:/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowControl.cs:105) UnityEditorInternal.AnimationWindowState.OnDisable () (at C:/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowState.cs:315)

NullReferenceException: Object reference not set to an instance of an object UnityEditorInternal.AnimationWindowControl.GetAnimationModeDriver () (at C:/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowControl.cs:474) UnityEditorInternal.AnimationWindowControl.get_previewing () (at C:/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowControl.cs:340) UnityEditorInternal.AnimationWindowControl.get_recording () (at C:/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowControl.cs:390) UnityEditorInternal.AnimationWindowControl.StopRecording () (at C:/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowControl.cs:421) UnityEditorInternal.AnimationWindowControl.StopPreview () (at C:/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowControl.cs:361) UnityEditorInternal.AnimationWindowControl.OnDisable () (at C:/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowControl.cs:105) UnityEditorInternal.AnimationWindowState.OnDisable () (at C:/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowState.cs:315) UnityEditor.AnimEditor.OnDisable () (at C:/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimEditor.cs:363) UnityEditor.AnimationWindow.OnDisable () (at C:/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindow.cs:72)

NullReferenceException: Object reference not set to an instance of an object UnityEditorInternal.AnimationWindowControl.GetAnimationModeDriver () (at C:/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowControl.cs:474) UnityEditorInternal.AnimationWindowControl.get_previewing () (at C:/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowControl.cs:340) UnityEditorInternal.AnimationWindowControl.get_recording () (at C:/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowControl.cs:390) UnityEditorInternal.AnimationWindowControl.StopRecording () (at C:/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowControl.cs:421) UnityEditorInternal.AnimationWindowControl.StopPreview () (at C:/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowControl.cs:361) UnityEditorInternal.AnimationWindowControl.OnDisable () (at C:/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowControl.cs:105) UnityEditorInternal.AnimationWindowState.OnDisable () (at C:/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowState.cs:315) UnityEditor.AnimEditor.OnDisable () (at C:/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimEditor.cs:363)

I don't know what any of this means and I can't find any other information on it. I haven't upgraded Unity or anything like that.

• Looks like you may have some files using UnityEditor that aren't under a folder called "editor". Can you search your codebase for "UnityEditor" and report back on what you find? – DMGregory Aug 8 '19 at 23:48
• @DMGregory I don't get anything in my project assets directory – Ultra Gamer Aug 9 '19 at 1:22
• What about Assets/Scripts/MainMenuManager.cs, line 41? – DMGregory Aug 9 '19 at 1:43
• @DMGregory That's one of my scripts, line 41 is a simple "UnityEditor.EditorApplication.isPlaying = false;" which acts as a quit button the editor. – Ultra Gamer Aug 9 '19 at 4:21
• @DMGregory This is going to sound very dumb, but commenting out that one line of code fixed it... Do you have any theories on why that line caused a problem in Unity but the game played in the editor and VS though it was fine? – Ultra Gamer Aug 9 '19 at 4:42

The main blocker here looks like:

Assets/Scripts/MainMenuManager.cs(41,21): error CS0234: The type or namespace name EditorApplication' does not exist in the namespace UnityEditor'. Are you missing an assembly reference?

It states that you have a script (MainMenuManager.cs) attempting to call code (EditorApplication) from the namespace UnityEditor, which does not exist.

It does not exist for your built game because the UnityEditor library, which contains that namespace, is not included in built games.

A workaround is using preprocessor define checks if you must have edit-time and runtime code in the same script, e.g.:

For editor-only code:

#if UNITY_EDITOR
using UnityEngine;
#endif


For using different code in the editor to any other platform (build target):

#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif


(note that in the above examples, the syntax highlighter colours lines beginning with # as comments. They are in fact the pertinent parts of the examples)

Unity has documentation on the support and use of these preprocessor defines for platform-dependant compilation. The preprocessor modifies the code sent to the compiler. When you build, #if UNITY_EDITOR is false and the compiler will never even see the code inside that block.

To explain why all may seem okay, Unity will share project files with Visual Studio which allow it to use the Visual Studio C# compiler for it's own functionality, e.g. error checking. To enable this functionality in VS for editor scripts, Unity includes a reference to the UnityEditor library for an 'editor' project to VS - making VS report that there are no errors because it's tools have a reference to the UnityEditor library whereas the Unity compiler that builds your game does not.

• Ok, but I have both "UnityEditor.EditorApplication.isPlaying = false;" and "Application.Quit();" in the same script already. – Ultra Gamer Aug 10 '19 at 1:58
• Did you notice the lines #if UNITY_EDITOR and similar? They are the ones which say which bits of code will get used. It may have been unclear as they are coloured the same as comments here. – Joe Aug 10 '19 at 9:06
• Ok, I see, thank you – Ultra Gamer Aug 12 '19 at 3:39