- Home /
Argument if out of range: loops and arrays
I keep getting this error. Argument is out of range. What does it mean?
Here is the code:
using System.Linq;
using System.Collections.Generic;
using UnityEngine;
namespace Assets.Scripts.Player
{
public class CameraScript : MonoBehaviour
{
Bounds b;
private new Transform transform;
private Vector3 DesiredPos;
public List<Transform> Players;
public float camSpeed;
private Camera cam;
public List <Transform> UpdatePlayers;
void Awake ()
{
transform = GetComponent<Transform>();
cam = GetComponent <Camera> ();
}
public void Start ()
{
b = new Bounds(new Vector2(0f, 0f), new Vector2(12f, 8f));
var p = GameObject.FindGameObjectsWithTag ("Player");
Players = new List<Transform> ();
for (int i = 0; i < p.Length; i++)
{
Players.Add(p[i].GetComponent<Transform>());
}
}
void Update ()
{
if (Players.Count <= 0)
{
return;
}
DesiredPos = Vector3.zero;
float distance = 0f;
var hSort = Players.OrderByDescending (p => p.position.y);
var wSort = Players.OrderByDescending (p => p.position.x);
var mHeight = hSort.First ().position.y - hSort.Last ().position.y;
var mWidth = wSort.First ().position.x - wSort.Last ().position.x;
var distanceH = -(mHeight + 5f) * 0.5f / Mathf.Tan (cam.fieldOfView * 0.5f * Mathf.Deg2Rad);
var distanceW = -(mWidth / cam.aspect + 5f) * 0.5f / Mathf.Tan (cam.fieldOfView * 0.5f * Mathf.Deg2Rad);
distance = distanceH < distanceW ? distanceH : distanceW;
for (int i = 0; i < Players.Count; i++)
{
DesiredPos += Players [i].position;
}
if (distance > -10f)
distance = -10f;
DesiredPos /= Players.Count;
DesiredPos.z = distance;
var k = GameObject.FindGameObjectsWithTag ("Player");
for (int l = 0;l < k.Length; l++)
{
if (b.Contains (k [l].transform.position))
{
Players.Add (k [l].GetComponent<Transform>());
}
else
{
Players.RemoveAt(l);
}
}
}
void LateUpdate ()
{
transform.position = Vector3.MoveTowards (transform.position, DesiredPos, camSpeed);
}
}
}
Copied it from the internet and tried to adapt it to my game.
Answer by Legend_Bacon · Mar 20, 2018 at 05:33 PM
Hello there,
It would be great if you could add a comment where the bug is coming from, so we can pinpoint the issue.
On another note, this error is extremely common and means that you're trying to extract a value from a list or array that just... doesn't have that value.
For example: if you have an array with 4 values and your code tries to get the 6th value from that array, you will get this error.
Try to debug the loop where the problem occurs: the solution will appear clear as day.
Hope that helps!
Cheers,
~LegendBacon
I don't get the error when i build and run the script, but only when i hit play,I get the error in the console window:
var k = GameObject.FindGameObjectsWithTag ("Player");
for (int l = 0;l < k.Length; l++)
{
if (b.Contains (k [l].transform.position))
{
Players.Add (k [l].GetComponent<Transform>());
}
else
{
Players.RemoveAt(l);
}
}
I belive it is because of these lines of script.
Answer by Priyanka-Rajwanshi · Mar 23, 2018 at 01:17 PM
It seems that the list 'Players' has a length less than that of 'k'. Thus when you call Players.RemoveAt(l); , an Argument out of range exception is thrown since the list 'Players' length would be less than 'l' and thus there is no element at index 'l'. You can write:
if(Players.Count > l) Players.RemoveAt(l);
Hope that would help!