How to attach camera to the instantiated gameobjects sequentially in a for loop using C# script?
"I have a bunch of FBX files. I want to make animation using these FBX files. Every FBX object have children. I need to Instantiate the first FBX file, then destroy that and Instantiate the second one and destroy, and do the same for all of them. I do this inside a for-loop. I attach the camera to the Instantiated game object's child and I need the camera follows the object, but the camera just attaches to the first object and stay in that position and does not attach to next Instantiated objects." (I haven’t forgooten to add this script to the main camera) This is part of C# code I have written. I don't know what's wrong? Could you please help?
public class Vector : MonoBehaviour {
private Vector3 offset=new Vector3(0,-2,0);
IEnumerator StreamLoad()
{
string[] filePaths = Directory.GetFiles(Application.dataPath + "/Stream/", "*.fbx");
for (int i = 0; i < filePaths.Length; i++);
{
string filename="Assets/Streamline/stream"+i.ToString("000")+".fbx";
GameObject go = (GameObject)AssetDatabase.LoadAssetAtPath(filename,typeof(GameObject));
GameObject myClone = (GameObject)GameObject.Instantiate(go, new Vector3(0, -2, -1),
Quaternion.identity);
GameObject childObj = myClone.transform.GetChild(2).gameObject;
transform.position = childObj.transform.position+offset;
GameObject.Destroy(myClone);
}
}
}
Your answer
Follow this Question
Related Questions
When instantiating 3 clones are created rather then 1 2 Answers
Instantiate and then destroy a prefab in a list. C# 1 Answer
Instantiate Previous Prefab in Array 0 Answers
Gameobject array 0 Answers