Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 mihai7887 · Mar 20, 2018 at 05:09 PM · arrayscamera-movementlistsloops

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.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Show 1 · 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 mihai7887 · Mar 20, 2018 at 06:09 PM 0
Share

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.

avatar image
0

Answer by Priyanka-Rajwanshi · Mar 23, 2018 at 01:17 PM

@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!

Comment
Add comment · 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

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

82 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Add existing variables to an array? 1 Answer

creating and manipulating a grid efficiently 1 Answer

What is the maximum amount of objects that can be stored in an Array or List without causing any error? 1 Answer

loop with array system 0 Answers

Displaying a List or an Array in Editor file 1 Answer


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