- Home /
Question by
areFranz · Nov 22, 2018 at 02:30 PM ·
animationcoroutinesnested
Lerp in Nested Coroutines doesn't work if I add some functions inside the Coroutines
Hi, I have a gameObject with some childs, and I need to move it forward, clear the childs, move back to original pos. The code I'm using is;
IEnumerator HidePrev ()
{
GameObject.Find("FileManager").GetComponent<FileManager>().lastPath = GameObject.Find("FileManager").GetComponent<FileManager>().curPath;
GameObject.Find("FileManager").GetComponent<FileManager>().curPath = transform.name;
float time = 0.0f;
Vector3 endPos = new Vector3(0.0f, 0.0f, 100.0f);
while (time < 1.0f)
{
time += Time.deltaTime;
GameObject.FindWithTag("Folder").transform.position = Vector3.Slerp(GameObject.FindWithTag("Folder").transform.position, endPos, (time / 1.0f));
yield return null;
}
}
IEnumerator ShowNext ()
{
yield return StartCoroutine(HidePrev());
//foreach (Transform child in GameObject.FindWithTag("Folder").transform)
//{
// Destroy(child.gameObject);
//}
float time = 0.0f;
Vector3 startPos = new Vector3(0.0f, 0.0f, 0.0f);
while (time < 1.0f)
{
time += Time.deltaTime;
GameObject.FindWithTag("Folder").transform.position = Vector3.Slerp(GameObject.FindWithTag("Folder").transform.position, startPos, (time / 1.0f));
yield return null;
}
//GameObject.Find("FileManager").GetComponent<FileManager>().showFiles();
//GameObject.Find("FileManager").GetComponent<FileManager>().showFolders();
}
In this way it works, but if I uncomment the commented lines, the ShowNext coroutine doesn't complete.. Any idea?
Comment
Answer by jenci1990 · Nov 22, 2018 at 09:04 PM
I think your code is runing at gameObject wtth tag "Folder", and it destroys itself there:
foreach (Transform child in GameObject.FindWithTag("Folder").transform) {
Destroy(child.gameObject); //destroy myself
}
$$anonymous$$hm, but the GameObject continues to exist but in the position Z 100...
Ok, try this:
IEnumerator HidePrev ()
{
GameObject.Find("File$$anonymous$$anager").GetComponent<File$$anonymous$$anager>().lastPath = GameObject.Find("File$$anonymous$$anager").GetComponent<File$$anonymous$$anager>().curPath;
GameObject.Find("File$$anonymous$$anager").GetComponent<File$$anonymous$$anager>().curPath = transform.name;
float time = 0.0f;
Vector3 endPos = new Vector3(0.0f, 0.0f, 100.0f);
while (time < 1.0f)
{
time += Time.deltaTime;
GameObject.FindWithTag("Folder").transform.position = Vector3.Slerp(GameObject.FindWithTag("Folder").transform.position, endPos, (time / 1.0f));
yield return null;
}
foreach (Transform child in GameObject.FindWithTag("Folder").transform)
{
Destroy(child.gameObject);
}
yield return null;
}
IEnumerator ShowNext ()
{
yield return StartCoroutine(HidePrev());
GameObject.Find("File$$anonymous$$anager").GetComponent<File$$anonymous$$anager>().showFiles();
GameObject.Find("File$$anonymous$$anager").GetComponent<File$$anonymous$$anager>().showFolders();
yield return null;
float time = 0.0f;
Vector3 startPos = new Vector3(0.0f, 0.0f, 0.0f);
while (time < 1.0f)
{
time += Time.deltaTime;
GameObject.FindWithTag("Folder").transform.position = Vector3.Slerp(GameObject.FindWithTag("Folder").transform.position, startPos, (time / 1.0f));
yield return null;
}
}
Excuse me for the late... It doesn't work. The folder GameObject remains at Z 100...