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*.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 .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());*
* }*
* 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.

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

@mihai7887

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!