I have scene where I want to spawn random character to random location with random walking waypoints. I have stripped these scripts so it would be easier to read.
Waypoints are stored in waypointmanager. Only parent transform will be stored. Here are the possible walking routes for character:
public class waypointManager : MonoBehaviour
{
#region Singleton
public static waypointManager instance;
#endregion
private void Awake()
{
instance = this;
}
public GameObject _waypointManager;
public Transform waypoints1;
public Transform waypoints2;
public Transform waypoints3;
public Transform waypoints4;
public Transform waypoints5;
}
Waypoints are controlled in another script. This is very stripped script but here is the most important parts:
public class civilanController : MonoBehaviour
{
// create npc waypoints
public List<Transform> waypointsCreated = new List<Transform>(); //waypointlist
private Transform targetWaypoint;
private int targetwaypointIndex = 0;
private float lastwaypointIndex;
void Update()
{
//make waypointloop for endless walking
lastwaypointIndex = waypointsCreated.Count - 1;
targetWaypoint = waypointsCreated[targetwaypointIndex]; //this is the line where error comes
}
}
Spawning these characters are in another script which is where I think the main problem is, even though console says error is in controller script.
public class inventoryBehaviour : MonoBehaviour
{
public Transform[] spawnPoints;
Transform waypointmanager1;
Transform waypointmanager2;
Transform waypointmanager3;
Transform waypointmanager4;
Transform waypointmanager5;
void Start()
{
waypointmanager1 = waypointManager.instance.waypoints1.transform;
waypointmanager2 = waypointManager.instance.waypoints2.transform;
waypointmanager3 = waypointManager.instance.waypoints3.transform;
waypointmanager4 = waypointManager.instance.waypoints4.transform;
waypointmanager5 = waypointManager.instance.waypoints5.transform;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
Countprefabs();
}
}
void Countprefabs()
{
for (int c = 0; c < inventoryManager.instance.inventoryList_civilians.Count; c++)
{
if (inventoryManager.instance.inventoryList_civilians[c].activeInHierarchy == false)
{
Transform randomTarget = spawnPoints[Random.Range(0, spawnPoints.Length)]; //store random spawnpoint
//This one doesnt work either
/* spawnPoints[0] = waypointmanager1.GetChild(0);
spawnPoints[1] = waypointmanager2.GetChild(0);
spawnPoints[2] = waypointmanager3.GetChild(0);
spawnPoints[3] = waypointmanager4.GetChild(0);
spawnPoints[4] = waypointmanager5.GetChild(0);
int spawnPointIndex = Random.Range(0, spawnPoints.Length); */
inventoryManager.instance.inventoryList_civilians[c].SetActive(true);
inventoryManager.instance.inventoryList_civilians[c].transform.position = randomTarget.position; //get random spawnpoint
inventoryManager.instance.inventoryList_civilians[c].transform.rotation = transform.rotation;
inventoryManager.instance.inventoryList_civilians.Remove(inventoryManager.instance.inventoryList_civilians[c]); //remove from the list
if (randomTarget == spawnPoints[0]) // if random spawnpoint is first of the spawnpoint list, make it match with wpmanager1
{
foreach (Transform wp1 in waypointmanager1) //goes through the list
{
inventoryManager.instance.inventoryList_civilians[c].GetComponent<civilanController>().waypointsCreated.Add(wp1);
}
return;
}
if (randomTarget == spawnPoints[1])
{
foreach (Transform wp2 in waypointmanager2)
{
inventoryManager.instance.inventoryList_civilians[c].GetComponent<civilanController>().waypointsCreated.Add(wp2);
}
return;
}
if (randomTarget == spawnPoints[2])
{
foreach (Transform wp3 in waypointmanager3)
{
inventoryManager.instance.inventoryList_civilians[c].GetComponent<civilanController>().waypointsCreated.Add(wp3);
}
return;
}
if (randomTarget == spawnPoints[3])
{
foreach (Transform wp4 in waypointmanager4)
{
inventoryManager.instance.inventoryList_civilians[c].GetComponent<civilanController>().waypointsCreated.Add(wp4);
}
return;
}
if (randomTarget == spawnPoints[4])
{
foreach (Transform wp5 in waypointmanager5)
{
inventoryManager.instance.inventoryList_civilians[c].GetComponent<civilanController>().waypointsCreated.Add(wp5);
}
return;
}
break;
}
}
}
}
Here is the error message: Index was out of range. Must be non-negative and less than the size of the collection parameter name:index
I do know what that error indicates, but don’t know why this isn’t working.