C# Error: Argument out of range

Hey Guys I have this script and I have been getting this error since I tried to make my character aiming by click “Fire2”:
ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index

I can’t locate the error I’m new on Unity3D :\

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

public class PlayerController : MonoBehaviour
{
public CharacterController CharCont;
public CharacterMotor CharMotor;

//Holders for Weapons
public Transform WalkAnimationHolder;
public Transform JumpAnimationHolder;
public Transform SwayAnimationHolder;
public Transform RecoilAnimationHolder;
public Transform ADSHolder;
//walking states
public WalkingState walkingstate = WalkingState.Idle;
public float VelocityMagnitude;
public float WalkSpeed;
public float RunSpeed;
public bool wasStanding;
//weapon settings
public WeaponInfo CurrentWeapon;
public List<WeaponInfo> WeaponList = new List<WeaponInfo>();
private float shoottime = 0;
public Vector3 CurrentRecoil1;
public Vector3 CurrentRecoil2;
public Vector3 CurrentRecoil3;
public Vector3 CurrentRecoil4;
//Player Var
public bool IsAiming;


void Start()
{
	CurrentWeapon = WeaponList[0];
}

public void Update()
{
	ShootController();
}

public void FixedUpdate()
{
	AnimationController();
	SwayController();
	SpeedController();
	VelocityMagnitude = CharCont.velocity.magnitude;
	RecoilController();
	ADSController();
}

//movement
public void SpeedController()
{
	if((Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0) && VelocityMagnitude > 0)
	{
		if(Input.GetButton("Run"))
		{
			walkingstate = WalkingState.Running;
			CharMotor.movement.maxForwardSpeed = RunSpeed;
			CharMotor.movement.maxSidewaysSpeed = RunSpeed;
			CharMotor.movement.maxBackwardsSpeed = RunSpeed / 2;
		}
		else
		{
			walkingstate = WalkingState.Walking;
			CharMotor.movement.maxForwardSpeed = WalkSpeed;
			CharMotor.movement.maxSidewaysSpeed = WalkSpeed;
			CharMotor.movement.maxBackwardsSpeed = WalkSpeed / 2;
		}
	}
	else
	{
		walkingstate = WalkingState.Idle;
	}
}

//Animations
public void AnimationController()
{
	if(wasStanding && !CharCont.isGrounded)
	{
		wasStanding = false;
		WalkAnimationHolder.animation.Play("animationSTANDINGJUMP");
	}
	else if (!wasStanding && CharCont.isGrounded)
	{
		wasStanding = true;
		WalkAnimationHolder.animation.Play("animationSTANDINGJUMPLANDING");
	}
	if(walkingstate == WalkingState.Running)
	{
		WalkAnimationHolder.animation["animationRUN"].speed = VelocityMagnitude / RunSpeed * 1.2f;
		WalkAnimationHolder.animation.CrossFade("animationRUN", 0.2f);
	}
	else if (walkingstate == WalkingState.Walking)
	{
		WalkAnimationHolder.animation["animationWALK"].speed = VelocityMagnitude / WalkSpeed * 1.2f;
		WalkAnimationHolder.animation.CrossFade("animationWALK", 0.2f);
	}
	else
	{
		WalkAnimationHolder.animation.CrossFade("animationIDLE", 0.2f);
	}
}

public void SwayController()
{
	
}

//Shoot Station
public void ShootController()
{
	if(Input.GetButton("Fire1") && walkingstate != WalkingState.Running)
	{
		if(shoottime <= Time.time)	
		{
			shoottime = Time.time + CurrentWeapon.firerate;
			CurrentRecoil1 += new Vector3(CurrentWeapon.RecoilRotation.x, Random.Range(-CurrentWeapon.RecoilRotation.y, CurrentWeapon.RecoilRotation.y));
			CurrentRecoil3 += new Vector3(Random.Range(-CurrentWeapon.RecoilKickback.x, CurrentWeapon.RecoilKickback.x), Random.Range(-CurrentWeapon.RecoilKickback.y, CurrentWeapon.RecoilKickback.y), CurrentWeapon.RecoilKickback.z);
			RaycastHit hit;
			if(Physics.Raycast(CurrentWeapon.Spawnpoint.position, CurrentWeapon.Spawnpoint.TransformDirection(Vector3.forward), out hit, 250));
			{
				hit.transform.SendMessageUpwards("GetBulletDamage", CurrentWeapon.name, SendMessageOptions.DontRequireReceiver);
			}
		}
	}
}

//weapon Recoil Station
public void RecoilController()
{
	CurrentRecoil1 = Vector3.Lerp(CurrentRecoil1, Vector3.zero, 0.1f);
	CurrentRecoil2 = Vector3.Lerp(CurrentRecoil2, CurrentRecoil1, 0.1f);
	CurrentRecoil3 = Vector3.Lerp(CurrentRecoil3, Vector3.zero, 0.1f);
	CurrentRecoil4 = Vector3.Lerp(CurrentRecoil4, CurrentRecoil3, 0.1f);
	RecoilAnimationHolder.localEulerAngles = CurrentRecoil2;
	RecoilAnimationHolder.localPosition = CurrentRecoil4;
}

//AimingDownSide
public void ADSController()
{
	 if (Input.GetButton("Fire2"))
    {
        IsAiming = true;
        ADSHolder.localPosition = Vector3.Lerp(ADSHolder.localPosition, CurrentWeapon.Scopes[CurrentWeapon.CurrentScope].adsposition, 0.25f);
    }
    else
    {
        IsAiming = false;
        ADSHolder.localPosition = Vector3.Lerp(ADSHolder.localPosition, Vector3.zero, 0.25f);
    }
}

}

public enum WalkingState
{
Idle,
Walking,
Running
}

//Weapon Info
[System.Serializable]
public class WeaponInfo
{
public string name = “Weapon”;
public float firerate = 0.1f;
public Transform WeaponTransform;
public Vector3 RecoilRotation;
public Vector3 RecoilKickback;
public Transform Spawnpoint;
public int CurrentScope;
public List Scopes = new List();
}

//Scope
[System.Serializable]
public class WeaponScope
{
public string name;
public float fov;
public Vector3 adsposition;
public Transform scopetransform;
}

It has to be the line that @Seregon mentioned

Just place a Debug.Log right before line 141:

Debug.Log("Current weapon scope count: " + CurrentWeapon.Scopes.Length + "  Current selected Scope index: " + CurrentWeapon.CurrentScope);

Run your game and press your Fire2 button and you will see that “CurrentWeapon.CurrentScope” is equal or larger than “CurrentWeapon.Scopes.Length”. So your actual error comes either from:

  • setting CurrentWeapon.CurrentScope to a wrong value
  • having the CurrentWeapon.Scopes array empty or simply smaller than you expect
  • not checking the index range before you use it

check Here: http://imageupload.co.uk/files/tn6vnozrih7xsuo27kmg.png
It’s an image which presents the error…
I repeat one more time this error appears ONLY in GameMode when i press Right Click (“Fire2”)