using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Waypoints : MonoBehaviour
{
public enum RotationMode
{
Normal, Curved
}
public GameObject objectToDuplicate;
public Transform[] waypoints;
public float movingSpeed;
public bool randomSpeed = false;
private GameObject[] prefabsToMove;
private void Start()
{
prefabsToMove = GameObject.FindGameObjectsWithTag("FrameLine");
DuplicatePrefabEffects(prefabsToMove.Length);
}
private void DuplicatePrefabEffects(int duplicationNumber)
{
for (int i = 0; i < duplicationNumber; i++)
{
var go = Instantiate(objectToDuplicate);
go.tag = "Duplicated Prefab";
go.name = "Duplicated Prefab";
}
}
private void WaypointsAI()
{
for (int i = 0; i < waypoints.Length; i++)
{
if (randomSpeed)
{
movingSpeed = Random.Range(1, 20);
}
prefabsToMove[i].transform.position = Vector3.MoveTowards(
prefabsToMove[i].transform.position,
waypoints[i].position,
movingSpeed * Time.deltaTime
);
}
}
void Update()
{
WaypointsAI();
}
}
The problem is inside the loop there might be less or more prefabsToMove then waypoints so it will throw exception.
Then doing prefabsToMove[i] is wrong.