- Home /
Error with nested coroutines
Hi All! I have a problem with a nested coroutine. I have a FileasAndFolders class that crates in the 3D scene a empty game object witch contains a series of icons for files and folders of the desktop of my mac. And sort of filevrowser in 3D. To navigate in and out the folders i use some coroutines (in the class IconClick that is attached to any icon in the scene), The problem is that this coroutine (that creates the object and the icons) works perfectly:
IEnumerator Load ()
{
showMenu = false;
Vector3 target = new Vector3 (0, 0, -50);
while (Vector3.Distance (cam.transform.position, target) > 0.25f) {
cam.transform.position = Vector3.Lerp (cam.transform.position, target, Time.deltaTime * 5f);
cam.transform.rotation = Quaternion.identity;
yield return null;
}
GameObject content = GameObject.FindWithTag ("content");
Destroy (content.gameObject);
DeskState.SetLastPath (DeskState.GetPath ());
DeskState.SetPath (DeskState.GetPath () + "/" + this.name);
FindObjectOfType <FoldersAndFiles> ().PopulateMainSpace (DeskState.GetPath ());
}
But the PreviousFoler coroutine returns me lots of empty objects with deifferent folders shown:
IEnumerator PrevFolder ()
{
Vector3 target = new Vector3 (0, 0, -50);
while (Vector3.Distance (cam.transform.position, target) > 0.25f) {
cam.transform.position = Vector3.Lerp (cam.transform.position, target, Time.deltaTime * 5f);
cam.transform.rotation = Quaternion.identity;
yield return null;
}
GameObject content = GameObject.FindGameObjectWithTag ("content");
Destroy (content.gameObject);
string prevPath = Directory.GetParent (DeskState.GetPath ()).FullName;
DeskState.SetPath (prevPath);
FindObjectOfType <FoldersAndFiles> ().PopulateMainSpace (DeskState.GetPath ());
}
If i delete the first while loop all it's ok but not animated... Any suggestion?
PS The DeskState class is a static class that I use to store the current visible folder path and the last showed path.
So, the coroutines are called by:
void Update ()
{
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Backspace) && DeskState.GetPath () != Environment.GetFolderPath(Environment.SpecialFolder.Desktop)) {
StartCoroutine (PrevFolder ());
}
}
And by a button in a GUI:
void OnGUI ()
{
GUI.skin = skin;
float width = Screen.width * 0.5f;
float height = Screen.height * 0.5f;
if (show$$anonymous$$enu) {
GUI.BeginGroup (new Rect (width - 256, height, 512, 256));
GUI.Box (new Rect (0, 0, 512, 256), this.name);
if(GUI.Button (new Rect (128, 112, 256, 32), "Open")) {
if (this.tag == "folder") {
StartCoroutine (Load ());
} else if (this.tag == "file") {
Process.Start (DeskState.GetPath () + "/" + this.name);
}
}
if (GUI.Button (new Rect (128, 160, 256, 32), "Back")) {
StartCoroutine (JumpBack ());
}
GUI.EndGroup ();
}
}
Sorry I forget... The first coroutine is called only if the folder is "Desktop" because i don't want to show previuos folders and this script is attached to every icon.
I don't really understand what's going on, but your scripts seem ok, no problems that I can tell. Try adding debug logs in the while loop that doesn't work, maybe that'll give you an idea as to what's going on. Print the camera's position, the target, etc.
I forget another thing... The scene is built by:
Empty Object "Content $$anonymous$$anager" with attached the script "FolderAndFiles.cs (That contains the Populate$$anonymous$$ainSpace(strig path) function, that creates an empty game object with the icons prefabs as child)
the relative "Content" empty object created by that function
the Camera with attached the Camera$$anonymous$$otion.cs class that makes the camera move around the space
the Structure game object that contains ad childs the six walls of the main cube
I've noticed that if i call Laod() and PrevoiusFolder() coroutines without moving the camera in the space with the mouse wheel, everything works, but if I load a folder, then move forward with $$anonymous$$ouseScrool Wheel, when i press "Backspace" key, somewhat creates more than one Content game object, the first with the Desktop icons, the second with the previous folder (personal folder), the third with the other previous folder (users folder) and the fourth with root folder...all with a "Null reference exception " error at line
string prevPath = Directory.GetParent (DeskState.GetPath ()).FullName;
The error is "Object reference not set to an instance of an object" I think that it could be an error due to the line:
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Backspace) && DeskState.GetPath () != Environment.GetFolderPath(Environment.SpecialFolder.Desktop))
and the time that the first whlie loop of PreviuosFolder() coroutine need to be completed... But how i can adjust that?
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Coroutine stops after any yield statement halfway through 0 Answers
Unity Follow backward 1 Answer
the name admob does not exist in the current context. 2 Answers