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 /
  • Help Room /
avatar image
0
Question by Serreas · Feb 29, 2016 at 06:30 PM · argumentoutofrangeexception

Argument out of range when looping through list

hi,

I'm having an issue with my code where it gives me an error saying argument is out of range and I can't seem to figure why its giving me that error because strangely enough the still does work as I intended. What I'm trying to do is create a straight line (with positions calculated along it) by placing control points with the mouse at runtime. The issue seems to come from the for loop starting at line 86. Here is the code:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;//Lists are in System.Collections.Generic
 
 public class RuntimeStraight : MonoBehaviour 
 {
     // Curve Points
     public Vector3 P1;
     public Vector3 P2;
     // List containing curve points
     public List<Vector3> PointList;
     //public List<Vector3> Nodes;
 
     // Nodes to instantiate
     public GameObject M1;
     //public Transform M2;
     private Vector3 p0;
     private Vector3 p1;
 
     // Set Color of Line Renderer
     public Color color = Color.red;
     // Sets Width of Line Renderer
     public float width = 0.2f;
 
     // Use this for initialization
     void Start () 
     {
         //Initializes Line Renderer component
         LineRenderer lineRenderer = GetComponent<LineRenderer> ();
         if (lineRenderer == null) 
         {
             gameObject.AddComponent<LineRenderer> (); 
         }
         lineRenderer = GetComponent<LineRenderer> ();
         // Makes Line to be rendered in World Space
         lineRenderer.useWorldSpace = true;
         // Sets Line shader
         lineRenderer.material = new Material(Shader.Find("Unlit/Texture"));
     }
 
 
     // Update is called once per frame
     void Update ()
     {
 
         if (Input.GetMouseButtonDown (0)) 
         {
 
             Ray ray = GetComponent<Camera> ().ScreenPointToRay (Input.mousePosition);
             RaycastHit hit;
             if (Physics.Raycast (ray, out hit, Mathf.Infinity)) 
             {
                 Debug.DrawRay (hit.point, hit.normal, Color.red, 5f);
                 PointList.Add (hit.point);//Adding the point to array
                 GameObject.Instantiate (M1, hit.point, Quaternion.identity);
     
                 /*if (PointList.Count == 1) 
                 {
                     P1 = new Vector3 (PointList [0].x, PointList [0].y, PointList [0].z);
                     //Transform.Instantiate (M1, P1, Quaternion.identity);
                     //Nodes.Add (P1);
                 }    
 
                 if (PointList.Count == 2) 
                 {
                     P2 = new Vector3 (PointList [1].x, PointList [1].y, PointList [1].z);
                     //Transform.Instantiate (M2, P2, Quaternion.identity);
                     //Nodes.Add (P2);
                 }
 
                 if (PointList.Count == 2) 
                 {
                     PointList.Clear ();
                 }*/
             }
         }
 
         LineRenderer lineRenderer = GetComponent<LineRenderer> ();
         if (lineRenderer == null || PointList.Count == 1) 
         {
             return; // no points specified
         } 
         Vector3 p0;
         Vector3 p1;
         int numberofPoints;
         for (int j = 0; j < PointList.Count; j++) {
             p0 = PointList [j];
             p1 = PointList [j + 1];
 
             float dist = Vector3.Distance (PointList [j], PointList [j + 1]);
             int RoundDist = Mathf.RoundToInt (dist);
             numberofPoints = RoundDist;
         
 
 
             // update line renderer
             lineRenderer.SetColors (color, color);
             lineRenderer.SetWidth (width, width);
             if (numberofPoints > 0) {
                 lineRenderer.SetVertexCount (numberofPoints);
             }
 
             float t;
             Vector3 position;
             for (int i = 0; i < numberofPoints; i++) {
                 t = i / (numberofPoints - 1f);
                 position = (1f - t) * p0;
                 position += t * p1;
                 lineRenderer.SetPosition (i, position);
             }
         }
     }
 }

and here is an image of the error. alt text

Hopefully somebody can help figure out whats causing this error. Any help is much appreciated.

capture.png (18.2 kB)
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

1 Reply

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

Answer by RobGraat · Feb 29, 2016 at 06:49 PM

Problem is with line 88 as the error report shows: .../RuntimeStraight.cs:88)

The variable j can be equal to the max of the listlength. Yet you look up a index equal to j+1. Which evaluates to listlength+1. Which is impossible.

Use

j < PointList.Count-1
to avoid the out of range error.

Cheers, Rob

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 Serreas · Feb 29, 2016 at 06:52 PM 0
Share

that worked perfectly thanks a bunch!

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

35 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

Related Questions

Inventory -Argument is out of range - Diagnostics to be done 1 Answer

Argument out of Range Exception Error. [Basic] 0 Answers

Argument is out of range whilst using a for loop. Yet when I insert indexes manually it works fine (C#) 1 Answer

Argument Out of Range Exception: Error with Array Turned into List 0 Answers

Argument out of range exception??? 0 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