- Home /
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; }
Theres far too much code here to go through and check everything. The most likely cause though, from a quick scan, is this line:
ADSHolder.localPosition = Vector3.Lerp(ADSHolder.localPosition, CurrentWeapon.Scopes[CurrentWeapon.CurrentScope].adsposition, 0.25f);
As your getting an error about an invalid index, I'd check that CurrentWeapon.Scopes[CurrentWeapon.CurrentScope] exists, i.e.: that CurrentWeapon.Scopes contains enough items and that CurrentWeapon.CurrentScope is >= 0 and < CurrentWeapon.Scopes.Length.
it doesnt specify any line.. the error is being appeared on playing mode when i press the right click "Fire2"
Did you define Fire2 in the Edit/Project settings/Input panel? It should be there by default but maybe your's was deleted
Fire2 Input Button is O$$anonymous$$ as i see.. propably something else is in charge of the problem. I also checked the line you wrote before. Nothing! I tried 2 more ways to do that and nothing happend it continues appearing the problem.. :\
Answer by george3d2011 · Jul 02, 2013 at 02:51 PM
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")
That showing exactly what we asked you to look for. Arrays in c# have a 0-based index, so that the first item has an index of 0, the second item is at 1, and the nth item is at n-1. As you only have on item in your array, you need to set CurrentWeapon.CurrentScope=0, not =1.
THAN$$anonymous$$S GUYS!!! really thank you! my problem is now fixed :) thank you!
Answer by Bunny83 · Jul 01, 2013 at 11:12 AM
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
When I type: Debug.Log("Current weapon scope count: " + CurrentWeapon.Scopes.Length + " Current selected Scope index: " + CurrentWeapon.CurrentScope); right before 141 Line it asks for a definition for Length ..
How can I make a definition for Length?? Sorry for chain-Questions but Im new on Unity3D and everything I $$anonymous$$ake is taken by Tutorials.. :\ Im trying to learn as more as I can from every source I find :\
when I type: Debug.Log("Current weapon scope count: " + CurrentWeapon.Scopes.Length + " Current selected Scope index: " + CurrentWeapon.CurrentScope);
right before 141 line.. It asks about a definition for Length
Assets/Script/PlayerController.cs(141,89): error CS1061: Type System.Collections.Generic.List' does not contain a definition for
Length' and no extension method Length' of type
System.Collections.Generic.List' could be found (are you missing a using directive or an assembly reference?)
How can I make a definition for Length?
sorry for chain-questions but Im new on Unity3D and Everything I do is from Tutorials.. I am trying to learn as more things as I can from every source I find :\
Since you didn't include your "WeaponInfo" class we don't know what type "Scopes" is. From your error message i can see that it's not an array but a generic List. So replace "Length" with "Count".
The Debug.Log should only help you to find your actual problem since you probably have a logically problem here.
I have included my weapon info list... But for one reason it hasnt been added with the whole script :\ So, anyway here it is :)
//Weapon Info [System.Serializable] public class WeaponInfo { public string name = "Weapon"; public float firerate = 0.1f; public Transform WeaponTransform; public Vector3 RecoilRotation; public Vector3 Recoil$$anonymous$$ickback; 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; }
I did everything you said.. the error about Length disappeared but it countinues not ai$$anonymous$$g
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Unexpected LAG :\ 1 Answer
How to import the object from server to unity 2 Answers
Rotating plane along z axis 0 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers