Application.dataPath is not working in build

Hi,

Initially I was using Application.dataPath to get the directory path so that I can load my textures dinamically in my application. It was working fine but as I started building my application it started showing error ‘Null reference’ for the Aplication.dataPath. So to overcome that problem I hard code the path of Asset folder and there was error. Build was successful. It was working fine. But now as I put the build in my friend PC and checked from there, then I found no texture was loading. I think it is the path problem because in my system application can find the texture path but in my friend PC it was not there.

Please help. How can I solve this problem. How can I map the path so that no error should be there.

Hi,

Please find the code. I am getting error in first line where I am creatinf path for the directory. I have hard coded here and it is not working in build. If I change path to Applicaiton.dataPath then it showing error (NullReference). May be because there is no such path. So how I will create path for directory.

Because I have read that Application.dataPath is Asset folder path for editor and AppName_Data folder path for build. So it it need to copy all asset in Resource folder in build folder (AppName_Data folder). If yes it will increase the burden to copy that data also in the perticular folder.

Code:


string path=“D:/Projects/MyNewGame/Assets”+folderPath+ Convert.ToString(drTexture[“textureFolder”]);
if(Directory.Exists(path))
{
string files=Directory.GetFiles(path);

objTextures[Index] = new Texture2D[files.Length];

int sIndex=0;
foreach(string element in files)
{
FileInfo objFileInfo = new FileInfo(element);
string name=objFileInfo.Name.Split(new char{‘.’});
objTextures[Index][sIndex]=(Texture2D)Resources.Load(subFolderPath+ Convert.ToString(drTexture[“textureFolder”])+“/”+name[0]);
sIndex++;
}
Index++;
}

enter code herehi_rahul86, your implementation is not the best.

Try to place your texture in Assets/Resources/Your_Folder.

Build your game. And use:

Resources.Load("Your_Folder/Your_texture_name", typeof(Texture2D))

It will work both in Editor and in your Built application

I solved this. Follow this workflow and you should be fine.

1 - Create a folder on your assets called “StreamingAssets”

2 - Use Application.streamingAssetsPath + “\” + yourFileName to access the string where your file is located.

Streaming assets sounds good, but it have some problems, on web because there is no file system access and android appliactions because it is compressed into the .apk file.

read more here:

Also “Resources” class with their “Loads” methods might be helpfull too:

I realize this is a very old question that was more-or-less answered but as this still came up high in the list when I google searched on this topic. I guess I’ll answer.

Application.dataPath returns a different path depending on if you are working in the Editor or on a built executable.

In the Editor, it returns the path for the Assets folder
In a built executable it returns the path to the built project’s Data folder (named [Project Name]_BuilderData).

So if you are placing your folder with the textures into the same folder as the executable file, just change the path in your code to: Application.dataPath + “/…” + “/[YOUR FOLDER PATH]/[YOUR FILE]”

On the other hand that will stop it from working in the Editor so if you want it to work in both, just leave your code as-is but place your dynamic textures into the data folder mentioned above.

Resources.Load doesn’t work if you want to be able to dynamically change the images without having to build every time.