ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System.Collections.Generic.List`1[UnityEngine.Vector3].get_Item (Int32 index) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:633) SphereBuilder.MoveShips () (at Assets/MyScripts/SphereBuilder.cs:75) SphereBuilder.Update () (at Assets/MyScripts/SphereBuilder.cs:65)
At the top of the script
private float distanceTravelled;
public bool updateOn = true;
private List<Vector3> lastPositions = new List<Vector3>();
private List<float> allDistances = new List<float>();
int countpos = 0;
In Start function
private void Start()
{
UpdateSpheres ();
spheres = GameObject.FindGameObjectsWithTag("MySphere");
foreach (Transform child in spheres[0].transform)
{
lastPosition = new Vector3(child.transform.position.x,child.transform.position.y,child.transform.position.z);
lastPositions.Add (lastPosition);
}
}
In Update function:
void Update()
{
if (updateOn == true) {
//whatever you want update to do.
foreach (Transform child in spheres[0].transform) {
child.transform.position += Vector3.forward * Time.deltaTime * moveSpeed;
Vector3 lp = lastPositions [countpos];
distanceTravelled += Vector3.Distance (child.transform.position, lp);
allDistances = new List<float> ();
allDistances.Add (distanceTravelled);
countpos ++;
}
}
if (countpos == spheres.Length)
{
updateOn = false;
distanceTravelled = 0;
countpos = 0;
}
// if you want certain parts of update to work at all times write them here.
foreach (Transform child in spheres[0].transform)
{
child.transform.position += Vector3.forward * Time.deltaTime * moveSpeed;
}
}
What i'm trying to do is to store the childs start positions and also to store the childs travelled distance.
Then somehow in another function maybe i want to loop each time over the childs and check if each child got to specific distance something like:
if (allDistances[0] >= 300000)
{
}
But instead allDistances[0] then in loop with a counter for example:
if (allDistances[counter] >= 300000)
{
}
The first problem is the out of range exception not sure why i'm getting it. The Spheres contain 20 childs.
The second problem is where and how to check each child for travelled distance each frame and when any of the childs get to 300000 do something with him.