I receive the following error when trying to build my game using unity:
The name
AssetDatabasedoes 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:
Either make sure all scripts that are only editor scripts are placed in folders called
Editor. Unity automatically excludes those in a build.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
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
pathin a Resources folder.This method returns the asset at
pathif it can be found, otherwise it returns null. Note that thepathis 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
pathis 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 calledAssets / Resources/andAssets / Guns / Resources/. The path does not need to includeAssetsandResourcesin the string, for example loading a GameObject atAssets / Guns / Resources / Shotgun.prefabwould only requireShotgunas the path. Also, ifAssets / Resources / Guns / Missiles / PlasmaGun.prefabexists it can be loaded usingGuns / Missiles / PlasmaGunas thepathstring. 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; }