Directory.getFiles() Not working in build
Ok, so the deal is that my VR proyects works just fine in the editor, but when I build it, the "Directory.getFiles()" method doesn't return the files I intend to get. The files I'm working on are outside the proyect's folder, so the idea is to get to use and download 3D models at runtime.
This is the piece of code:
try {
var path = Application.dataPath + "/StreamingAssets";
DirectoryInfo directory = new DirectoryInfo(path);
FileInfo[] directoryInfo = directory.GetFiles();
int i = 0;
foreach (var file in directoryInfo)
{
if (file.Extension == ".obj")
{
objectsInFolder.Add(OBJLoader.LoadOBJFile(file.FullName));
objectsInFolder[i].transform.position = new Vector3(0f, -5000f, 0f);
objectsInFolder[i].AddComponent<VRTK_InteractableObject>();
objectsInFolder[i].AddComponent<VRTK_FixedJointGrabAttach>();
objectsInFolder[i].AddComponent<VRTK_AxisScaleGrabAction>();
objectsInFolder[i].AddComponent<Rigidbody>();
var vrtkInteract = objectsInFolder[i].GetComponent<VRTK_InteractableObject>();
vrtkInteract.touchHighlightColor = Color.yellow;
vrtkInteract.isGrabbable = true;
vrtkInteract.grabAttachMechanicScript = vrtkInteract.transform.GetComponent<VRTK_FixedJointGrabAttach>();
vrtkInteract.secondaryGrabActionScript = vrtkInteract.transform.GetComponent<VRTK_AxisScaleGrabAction>();
var vrtkFixedJoint = objectsInFolder[i].GetComponent<VRTK_FixedJointGrabAttach>();
vrtkFixedJoint.precisionGrab = true;
var vrtkAxisScaleGrab = objectsInFolder[i].GetComponent<VRTK_AxisScaleGrabAction>();
vrtkAxisScaleGrab.uniformScaling = true;
try
{
for (int j = 0; j < objectsInFolder[i].transform.childCount; j++)
{
objectsInFolder[i].transform.GetChild(j).gameObject.AddComponent<MeshCollider>();
objectsInFolder[i].transform.GetChild(j).gameObject.GetComponent<MeshCollider>().convex = true;
objectsInFolder[i].transform.GetChild(j).transform.localScale = new Vector3(0.001f, 0.001f, 0.001f);
objectsInFolder[i].transform.GetChild(j).transform.localPosition = new Vector3(0, 0, 0);
//objectsInFolder[i].transform.GetChild(j).gameObject.AddComponent<Rigidbody>();
}
objectsInFolder[i].AddComponent<InteractableObject_Hook_Script>();
int f = int.Parse(i.ToString());
buttonsInScene[i].onClick.AddListener(delegate { objSpawner.InstantiateObject(objectsInFolder[f]); });
buttonsInScene[i].transform.GetChild(0).GetComponent<Text>().text = file.Name.Replace(".obj", "").Replace("IKEA", "").Replace("3D", "").Replace("-", "").Replace("_", " ").ToUpper();
i++;
}
catch (Exception e)
{
print("Error: " + e);
}
}
}
}
catch (Exception ex)
{
debugText.text = ex.Message;
}
Answer by $$anonymous$$ · Aug 02, 2017 at 06:25 PM
It seems that the problem is with the path that you're creating the directory with. I think that the Application.dataPath variable is editor only, as it's a path to the Editor Assets folder (which the user won't have access to). To get it to work in the build, try replacing Application.dataPath with Application.streamingAssetsPath. I've never used it, but by looking at the docs, it looks like it might work.
Something else that might also be of use to you is this Unity Answer, which talks more in depth about some of the other dataPath's and their particular uses.
Hope this helps to solve your problem!
Your answer
![](https://koobas.hobune.stream/wayback/20220612131708im_/https://answers.unity.com/themes/thub/images/avi.jpg)