x


Argument is out of range in array

oksy so I am creating a script to shuffle close targets

here is my code

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Beta_targeting : MonoBehaviour
{
    public Transform selectedTarget;
    public List<Transform> targets;

    void Start ()
    {
        targets = new List<Transform>();
    }

    public IList<Transform> FindEnemiesInSphere(float radius)
    { 
        Collider[] cols = Physics.OverlapSphere(transform.position, 10);
        SortedList<float, Transform> transforms = new SortedList<float, Transform>();
        foreach (Collider hit in cols) {
            if (hit && hit.tag == "Enemy"){              
                float Dist = Vector3.Distance(transform.position, hit.transform.position);
                transforms.Add(Dist, hit.transform); 
            }
        }
        foreach(Transform trans in FindEnemiesInSphere(6.0f))
        {
            targets.Add(trans);
        }
        return transforms.Values;
    }

    void Update ()
    {
        if(Input.GetKeyDown(KeyCode.Tab))
            choosingtarget();
    }

    public void choosingtarget()
    {
        if(selectedTarget == null) {
            selectedTarget = targets[0];
        }
        else {
            int index = targets.IndexOf(selectedTarget);
            if(index < targets.Count - 1) {
                index++;
            }
            else {
                index = 0;
            }
            selectedTarget = targets[index];
        }
    }   
}

however I get an error

"Argument is out of range. Parameter name: index"

any help?

more ▼

asked Nov 09 '11 at 01:15 AM

Babilinski gravatar image

Babilinski
84 40 42 43

I've reformatted your code because it was really hard to see what belongs to what.

As you can see you don't call FindEnemiesInSphere anywhere except in the function itself!!!! This would lead to a Stack overflow because you would recursivly call the function. Good for you that you don't call the function at the moment, but it should be called (after you fixed it of course) or your targets - List will stay empty like syclamoth said.

Nov 09 '11 at 04:25 AM Bunny83

I get the feeling that @Babilinski is fundamentally not understanding the point of that function (which I wrote earlier today, as a solution to a different problem). The bit in the middle which calls the function inside itself is an addition to my script, which kind of screws everything up!

Nov 09 '11 at 04:34 AM syclamoth

Ok, just to keep the information chain:

http://answers.unity3d.com/questions/184053/targeting-multiple-enemys.html

(i found it in your answers ;))

Nov 09 '11 at 04:45 AM Bunny83
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

The problem is that you never actually assign anything to 'targets'! Which means that index '0' will be out of range, because targets is empty. You need to have a line in there somewhere which goes like-

targets = FindEnemiesInSphere(radius);

before you try to access any of its members.

Also, what is this line doing in your FindEnemiesInSphere function?

foreach(Transform trans in FindEnemiesInSphere(6.0f))
{
      targets.Add(trans);
}

That really shouldn't be there. I don't think you understood what the point of that method was when I wrote it for you earlier.

more ▼

answered Nov 09 '11 at 01:47 AM

syclamoth gravatar image

syclamoth
14.8k 7 15 80

the list targets does not get any new transforms. do you know why that is?

Nov 09 '11 at 01:54 AM Babilinski

I just explained it! What part of my post can't you understand?

Nov 09 '11 at 03:02 AM syclamoth

I understand all of it. When I hit play and hit the button it still says 0

Nov 09 '11 at 03:08 AM Babilinski

Can you post your 'Start' function? The problem here is that it should be assigned instantaneously- if you check for enemies when there are none, and then look at the result of that check later on when there are enemies around, it'll still show zero enemies until you update it again! You need to put the

targets = FindEnemiesInSphere(radius);

line in every time you want to use it.

Nov 09 '11 at 03:42 AM syclamoth

I guess the

foreach(Transform trans in FindEnemiesInSphere(6.0f))

loop should be inside choosingtarget at the beginning. Also make sure you clear your targets list before adding your targets. If you call this function several times it would add-up the same targets multiple times and the targets list would grow until you run out of memory :D

Nov 09 '11 at 04:31 AM Bunny83
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1949
x1359
x1279
x131

asked: Nov 09 '11 at 01:15 AM

Seen: 1362 times

Last Updated: Nov 09 '11 at 04:45 AM