I can currently Instantiate the GameObject just fine, but I have a script attached to it that has an array for waypoints that I would like the object to move back and forth to. In the editor I was able to manually set waypoints by dragging empty gameobjects into the array, but I would prefer to instantiate these empty objects and set their transform.position via this BuildLevel script instead. This is the part I am stuck on. The goal is to be able to have multiple enemies each with their own waypoints to patrol.
Here is the code I am using to instantiate my GameObject(some code is snipped):
public GameObject Enemy2Way;
public void BuildLevel(int whatLevel)
{
if (whatLevel == 0)
{
Instantiate (Enemy2Way, new Vector3 (0, 1, 5), Quaternion.identity);
}
}
And here is the script that is attached to the Enemy2Way gameobject:
public Transform[] patrolPoints;
private int currentPoint;
public float moveSpeed;
void Start () {
transform.position = patrolPoints [0].position;
currentPoint = 0;
}
void Update () {
if (transform.position == patrolPoints [currentPoint].position)
{
currentPoint++;
}
if (currentPoint >= patrolPoints.Length)
{
currentPoint = 0;
}
transform.position = Vector3.MoveTowards (transform.position, patrolPoints [currentPoint].position, moveSpeed * Time.deltaTime);
}
}