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

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()

3 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 
0

AssetDatabase only works in the editor. You can use Resources.Load instead.

For example:

var jsonTextFile = Resources.Load<TextAsset>("Text/jsonFile01");

From Unity's Resources.Load documentation:

Loads the asset of the requested type stored at path in a Resources folder.

This method returns the asset at path if it can be found, otherwise it returns null. Note that the path is case insensitive and must not contain a file extension. All asset names and paths in Unity use forward slashes, so using backslashes in the path will not work.

The path is relative to any folder named Resources inside the Assets folder of your project. More than one Resources folder can be used. If you have multiple Resources folders you cannot duplicate the use of an asset name. For example, a project may have Resources folders called Assets / Resources/ and Assets / Guns / Resources/. The path does not need to include Assets and Resources in the string, for example loading a GameObject at Assets / Guns / Resources / Shotgun.prefab would only require Shotgun as the path. Also, if Assets / Resources / Guns / Missiles / PlasmaGun.prefab exists it can be loaded using Guns / Missiles / PlasmaGun as the path string. If you have multiple Resources folders you cannot duplicate the use of an asset name.

Use the code below and it will work fine. Make sure your file is inside the "Resources" folder. Add the name without the extension of the file. This solution works for all cases.

Object[] GetSprites(string fileName) { Object[] sprites = Resources.LoadAll(fileName); return sprites; } 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like