- Home /
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?
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.
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!
Ok, just to keep the information chain:
http://answers.unity3d.com/questions/184053/targeting-multiple-enemys.html
(i found it in your answers ;))
Answer by syclamoth · Nov 09, 2011 at 01:47 AM
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.
the list targets does not get any new transforms. do you know why that is?
I just explained it! What part of my post can't you understand?
I understand all of it. When I hit play and hit the button it still says 0
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.
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