So basically I’m making a VR simulation. I have an object with several children in the scene. Throughout this object is a series of Node objects that link parents and children. The basic idea is that I find a child, and then traverse the Nodes back to the root node, copy this traversal to a list, and then follow the list in sequence playing animations and turning on components making the objects grabbable.
I have a menu that has several buttons which pass a string which is the name of a piece, and then call an Animate(string endingObject) function on my manager. Each time a button is pressed on my menu I’m destroying the current object in the scene and instantiating a new one.
What ends up happening though is my first run through the animation plays fine and my manager properly gets the reference to the ending point of my animation sequence, and subsequently is able to build the list out to the starting point. When I instantiate a second copy of the object though (after deleting the first) and attempt to pass it a new ending component, all I get is a bunch of missing GameObjects on my manager. If I run a debug however on the components I’m able to pull the proper object names.
My best guess as to what is happening is that my manager is updating before the old object is deleted, but when I’ve inserted pauses into the code to test that theory it still has the same behavior.
I’m not quite sure what I’m doing wrong here.
Here’s the script in question (sorry for the messy code)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
public class AnimationPathfinder : MonoBehaviour {
public GameObject startingPoint;
public GameObject endingPoint;
public List<GameObject> path = new List<GameObject>();
public GameObject currentObject;
public int spawned = 0;
public GameObject iBrightPrefab;
public GameObject iBright = null;
IEnumerator playAnimationPath() {
// Debug.Log ("Playing animation path");
for (int i = 0; i < path.Count; i++) {
//Debug.Log (path [i].name);
path [i].GetComponent<PlayableDirector> ().Play ();
float timeToWait = (float)path [i].GetComponent<PlayableDirector> ().duration;
yield return new WaitForSeconds (timeToWait);
}
yield return null;
}
public void Animate(string ending) {
//Debug.Log ("Building animation path");
StopCoroutine(playAnimationPath());
resetUnit ();
endingPoint = GameObject.Find (ending);
currentObject = endingPoint;
path = new List<GameObject> ();
//Debug.Log ("ending point is active " + endingPoint.activeSelf);
while (currentObject.GetComponent<Node> ().parent != null) {
//Debug.Log ("Current Point is " + currentObject.name);
path.Insert (0, currentObject);
//currentObject.GetComponent<PlayableDirector> ().timeUpdateMode = DirectorUpdateMode.Manual;
currentObject = currentObject.GetComponent<Node> ().parent;
}
//Debug.Log ("Path built");
//Debug.Log ("Current Point is " + currentObject.name);
startingPoint = currentObject;
path.Insert (0, startingPoint);
StartCoroutine (playAnimationPath ());
}
public void resetUnit() {
if (iBrightPrefab == null) {
iBrightPrefab = Resources.Load ("Prefabs/Animation IBright") as GameObject;
}
if (spawned == 1) {
Debug.Log ("Attempting to destroy");
GameObject target = GameObject.Find ("Animation IBright(Clone)");
Destroy (target);
spawned = 0;
}
if (spawned == 0) {
iBright = GameObject.Instantiate (iBrightPrefab, new Vector3 (-2.136f, .199f, .620f), Quaternion.identity);
spawned = 1;
}
}
}