When building your plugin, you will often find a number of compilation errors in the console, even though your code ran fine when testing in the editor - so what’s going on, and how do we fix it?

In Unity, sometimes you’ll find code that is only allowed to be called from an editor context, and not from the build. If you’re on this page, it is likely your scripts contain this code. Once your build has failed, open the console and look for compilation errors. These will likely come in two categories

1. Erroneous using statements

IDEs like Visual Studio , VSCode, Ryder etc have a habit of automatically adding imports to your files (Often when you mistype some code, the IDE predicts you’re asking for some obscure library, and “helpfully” imports it for you). If these namespaces are editor-only, even if they’re not actually being used by your code, they will cause compilation errors. Some examples of common bad imports include

Fortunately, the fix is simple, simply remove these imports, and retry the build.

2. Calls to the UnityEditor namespace

The UnityEditor namespace provides many helpful utilities that can be used in the editor, but not in the build. Removing the import may not be enough if your code actually is using this namespace

Sometimes, this code can be replaced with a build-friendly equivalent, like replacing UnityEditor.AssetDatabase.LoadAssetAtPath with Resources.Load, as in the example below.

If there is no build-friendly substitute, and you don’t want to remove this code each time you run a build, you can surround it with #if UNITY_EDITOR tags to ensure it only compiles when running in an editor context, as in the example below.

image.png