Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by george3d2011 · Jun 30, 2013 at 06:42 PM · aiming

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; }

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Oribow · Jun 30, 2013 at 07:00 PM 0
Share

in which line is the Error?

avatar image Seregon · Jun 30, 2013 at 07:02 PM 2
Share

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.

avatar image george3d2011 · Jun 30, 2013 at 08:28 PM 0
Share

it doesnt specify any line.. the error is being appeared on playing mode when i press the right click "Fire2"

avatar image DaveA · Jun 30, 2013 at 08:43 PM 0
Share

Did you define Fire2 in the Edit/Project settings/Input panel? It should be there by default but maybe your's was deleted

avatar image george3d2011 · Jul 01, 2013 at 10:54 AM 0
Share

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.. :\

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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")

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Seregon · Jul 02, 2013 at 02:54 PM 1
Share

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.

avatar image Bunny83 · Jul 02, 2013 at 03:32 PM 1
Share

Exactly. See zero based indexing

avatar image george3d2011 · Jul 02, 2013 at 05:11 PM 0
Share

THAN$$anonymous$$S GUYS!!! really thank you! my problem is now fixed :) thank you!

avatar image
1

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

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image george3d2011 · Jul 01, 2013 at 04:04 PM 0
Share

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 :\

avatar image george3d2011 · Jul 01, 2013 at 04:14 PM 0
Share

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 :\

avatar image Bunny83 · Jul 01, 2013 at 09:29 PM 1
Share

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.

avatar image george3d2011 · Jul 02, 2013 at 08:31 AM 0
Share

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; }

avatar image george3d2011 · Jul 02, 2013 at 08:40 AM 0
Share

I did everything you said.. the error about Length disappeared but it countinues not ai$$anonymous$$g

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges