I’m trying to make a multitude of instantiated objects that will follow my FirstPersonController around when I walk. This FristPersonController has a capsule object as a child, so the character has some visibility.
For this, I have a gameBrain with the following script assigned to it:
public class gameBrain : MonoBehaviour {
void Start () {
GameObject prefab = Resources.Load ("EthanScript") as GameObject;
for (int i = 0; i < 100; i++) {
GameObject go = Instantiate (prefab) as GameObject;
go.transform.position = new Vector3 (i*0.10f, 0, i*2);
}
}
}
The object EthanScript has a script attached to it that makes it follow any object you assign to it.
public class LootAtFollow : MonoBehaviour {
public Transform myTarget;
public float mySpeed = 10.0f;
public float epsilon = 0.1f;
void Update () {
transform.LookAt (myTarget.position);
if ((transform.position - myTarget.position).magnitude > epsilon) {
transform.Translate (0.0f, 0.0f, mySpeed * Time.deltaTime);
}
}
}
Now I want to immediately assign the capsule that is my controller to the instantiated objects. How do I access the capsule (child of Controller) in code and assign it to be the myTarget? I would like this to be done in code, because I don’t want to have to drag and drop the capsule a hundred times into the myTarget slot in the Inspector.
I tried multiple things but keep running into multiple errors. I hope someone has a tip for me! Thanks in advance!