-1

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.

TheLost Lostit
  • 505
  • 6
  • 28

1 Answers1

1

I propose to use a if clause to check whether countpos does not exceed the length of the list:

foreach (Transform child in spheres[0].transform) {
   child.transform.position += Vector3.forward * Time.deltaTime * moveSpeed;

   if (countpos < lastPositions.Count() && countpos >= 0) {
        Vector3 lp = lastPositions [countpos];
        distanceTravelled += Vector3.Distance (child.transform.position, lp);
        allDistances = new List<float> ();
        allDistances.Add (distanceTravelled);
        countpos ++;
   }
}
rbr94
  • 2,227
  • 3
  • 23
  • 39
  • This IF not working good. I used a break point. First time it does adding the distanceTravelled to the List allDistances it does enter the IF. But then i click continue and i see that countpos value is already 20. So it's never enter again and the List is 1 lenght. What i want to do is to make new instance to the List maybe not in this place so it will add to it each time the last travelled distance of all childs. – TheLost Lostit Oct 08 '16 at 12:39
  • Then use `else` to do this. But the `if` prevents you from receiving an `ArgumentNullReferenceException` – rbr94 Oct 08 '16 at 13:03
  • I did inside the foreach if (countpos == lastPositions.Count - 1) countpos = 0; and it's working now. It's adding the first time the 19 childs travelled distances to the List then make new instance and adding the next distances to the List. This way it's updating each time the last travelled distances. – TheLost Lostit Oct 08 '16 at 13:17