Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UNITY - The name `AssetDatabase' does not exist in the current context

Tags:

unity3d

I receive the following error when trying to build my game using unity:

The name AssetDatabase does not exist in the current context

Can anyone please help.. I will appreciate all the help I can get..thank you

Build completed with a result of 'Failed' UnityEditor.BuildPlayerWindow:BuildPlayerAndRun()

UnityEditor.BuildPlayerWindow+BuildMethodException: 5 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 UnityEditor.BuildPlayerWindow:BuildPlayerAndRun()

like image 878
Sam Avatar asked Apr 02 '19 16:04

Sam


1 Answers

It seems that in one of your scripts you use AssetDatabase which belongs to the UnityEditor namespace. It exists only inside the Unity Editor.

In a build the namespace UnityEditor does not exists -> You can't use anything of it in a built application!


So after beeing aware of that there are basically to solutions how to fix those errors when using e.g. custom editor scripts or some code blocks that shall only exist inside of the Unity Editor but not in a build:

  1. Either make sure all scripts that are only editor scripts are placed in folders called Editor. Unity automatically excludes those in a build.

  2. Or use the #if pre-processor with UNITY_EDITOR:

    #if UNITY_EDITOR
        using UnityEditor;
    #endif
    
    ...
    
    #if UNITY_EDITOR
        //some code here that uses something from the UnityEditor namespace
    #endif
    
like image 65
derHugo Avatar answered Oct 19 '22 22:10

derHugo