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 samyn26 · Nov 10, 2016 at 08:12 AM · listargumentexception

How to Repair Argument is Out of Range

Hello, this is the first time I ask in this forum. I hope you all can help me. Thank you.

I really confuse with this error because this is the first I make little bit serious project in unity, so I confuse with the error. This error is appear after the line in screen is drawn.

This is the picture of error : alt text

And this is the code. I use someone code to the draw function and then add code Monotone Chain Convex Hull algorithm for check the convex hull of line which drawn. Anyone can help me please ?

And this is the full code :

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 
 public class DrawLine : MonoBehaviour 
 {
     private LineRenderer line;
     private bool isMousePressed;
     private List<Vector3> pointsList;
     private List<Vector3> H;
     private List<Vector3> simpanConvex;
     private List<Vector3> convexPlus;
     private Vector3 mousePos;
 
     // Structure for line points
     struct myLine
     {
         public Vector3 StartPoint;
         public Vector3 EndPoint;
     };
     //    -----------------------------------    
     void Awake()
     {
         // Create line renderer component and set its property
         line = gameObject.AddComponent<LineRenderer>();
         line.material =  new Material(Shader.Find("Particles/Multiply"));
         line.SetVertexCount(0);
         line.SetWidth(0.1f,0.1f);
         line.SetColors(Color.green, Color.yellow);
         line.useWorldSpace = true;    
         isMousePressed = false;
         pointsList = new List<Vector3>();
 //        renderer.material.SetTextureOffset(
     }
     //    -----------------------------------    
     void Update () 
     {
         // If mouse button down, remove old line and set its color to green
         if(Input.GetMouseButtonDown(0))
         {
             isMousePressed = true;
             line.SetVertexCount(0);
             pointsList.RemoveRange(0,pointsList.Count);
             line.SetColors(Color.green, Color.yellow);
         }
         else if(Input.GetMouseButtonUp(0))
         {
             isMousePressed = false;
         }
         // Drawing line when mouse is moving(presses)
         if(isMousePressed)
         {
             mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             mousePos.z=0;
             if (!pointsList.Contains (mousePos)) 
             {
                 pointsList.Add (mousePos);
                 line.SetVertexCount (pointsList.Count);
                 line.SetPosition (pointsList.Count - 1, (Vector3)pointsList [pointsList.Count - 1]);
                 if(isLineCollide())
                 {
                     isMousePressed = false;
                     line.SetColors(Color.red, Color.red);
 
                     //proses Algoritma
                     simpanConvex = new List<Vector3>();
                     simpanConvex = convex_hull(pointsList);
                     pointsList.Add (new Vector3(2.23f,-0.89f,0));
                     convexPlus = new List<Vector3>();
                     convexPlus = convex_hull(pointsList);
 
                     //proses cek objek di dalam atau di luar
                     for (int i = 0; i < convexPlus.Count; i++)
                     {
                         if ((simpanConvex[i].x == convexPlus[i].x) && (simpanConvex[i].y == convexPlus[i].y))
                         {
                             Debug.Log ("Didalam");
                         }
                         else
                         {
                             Debug.Log ("Diluar");
                         }
                     }
                 }
             }
         }
     }
     //    -----------------------------------    
     //  Following method checks is currentLine(line drawn by last two points) collided with line 
     //    -----------------------------------    
     private bool isLineCollide()
     {
         if (pointsList.Count < 2)
             return false;
         int TotalLines = pointsList.Count - 1;
         myLine[] lines = new myLine[TotalLines];
         if (TotalLines > 1) 
         {
             for (int i=0; i<TotalLines; i++) 
             {
                 lines [i].StartPoint = (Vector3)pointsList [i];
                 lines [i].EndPoint = (Vector3)pointsList [i + 1];
             }
         }
         for (int i=0; i<TotalLines-1; i++) 
         {
             myLine currentLine;
             currentLine.StartPoint = (Vector3)pointsList [pointsList.Count - 2];
             currentLine.EndPoint = (Vector3)pointsList [pointsList.Count - 1];
             if (isLinesIntersect (lines [i], currentLine)) 
                 return true;
         }
         return false;
     }
     //    -----------------------------------    
     //    Following method checks whether given two points are same or not
     //    -----------------------------------    
     private bool checkPoints (Vector3 pointA, Vector3 pointB)
     {
         return (pointA.x == pointB.x && pointA.y == pointB.y);
     }
     //    -----------------------------------    
     //    Following method checks whether given two line intersect or not
     //    -----------------------------------    
     private bool isLinesIntersect (myLine L1, myLine L2)
     {
         if (checkPoints (L1.StartPoint, L2.StartPoint) ||
             checkPoints (L1.StartPoint, L2.EndPoint) ||
             checkPoints (L1.EndPoint, L2.StartPoint) ||
             checkPoints (L1.EndPoint, L2.EndPoint))
             return false;
         
         return((Mathf.Max (L1.StartPoint.x, L1.EndPoint.x) >= Mathf.Min (L2.StartPoint.x, L2.EndPoint.x)) &&
                (Mathf.Max (L2.StartPoint.x, L2.EndPoint.x) >= Mathf.Min (L1.StartPoint.x, L1.EndPoint.x)) &&
                (Mathf.Max (L1.StartPoint.y, L1.EndPoint.y) >= Mathf.Min (L2.StartPoint.y, L2.EndPoint.y)) &&
                (Mathf.Max (L2.StartPoint.y, L2.EndPoint.y) >= Mathf.Min (L1.StartPoint.y, L1.EndPoint.y)) 
                );
     }
 
     //fungsi cross algoritma
     private double cross (Vector3 o, Vector3 a, Vector3 b)
     {
         return ((a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x));
     }
 
     //fungsi sort
     private int Vector3Compare (Vector3 v1, Vector3 v2)
     {
         if (v1.x < v2.x) {
             return -1;
         } else if (v1.x == v2.x) {
             if (v1.y < v2.y) {
                 return -1;
             } else if (v1.y == v2.y) {
                 if (v1.z < v2.z) {
                     return -1;
                 } else if (v1.z == v2.z) {
                     return 0;
                 } else {
                     return 1;
                 }
             } else {
                 return 1;
             }
         } else {
             return 1;
         }
     }
 
     //fungsi Algoritma
     private List<Vector3> convex_hull (List<Vector3> listCome)
     {
         int n = listCome.Count;
         int k = 0;
         H = new List<Vector3>();
         //sort
         //listCome.Sort ();
         //listCome = listCome.OrderBy (v => v.x).OrderByDescending (v => v.y);
         listCome.Sort (Vector3Compare);
         //build lower hull
         for (int i = 0; i < n; i++) {
             while (k >= 2 && cross(H[k-2], H[k-1], listCome[i]) <= 0) k--;
             H[k++] = listCome[i];
         }
 
         //build upper hull
         for (int i = n-2, t = k+1; i >= 0; i--) {
             while (k >= t && cross(H[k-2], H[k-1], listCome[i]) <= 0) k--;
             H[k++] = listCome[i];
         }
         //resize dipertanyakan
         //H.Capacity.Equals (k);
 
         return H;
     }
 
 }

screenshot-299.png (176.6 kB)
Comment
Add comment · Show 4
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 Sergio7888 · Nov 10, 2016 at 10:02 PM 1
Share

the problem is k value, use a Debug.Log(k); to see the value of k before the H[k++] = listCome[i]; on line 184. In a List you can't call List[index] where the index is greater or equal than the List Count value or the index is less than 0.

Edit: C# list can only use index less than List Count, if you want use a index greater than count you need use a array, but using a array you will need specify the array size when you create the array, and you will need check if the index is less than array Length before set the value to index, if the index is equal or greater than Length you can use Array.Resize to change array Length.

avatar image Bunny83 Sergio7888 · Nov 11, 2016 at 01:23 AM 0
Share

Right, the list "H" is empty so you can't access a specific element as it doesn't contain any elements.

avatar image samyn26 Sergio7888 · Nov 11, 2016 at 02:07 AM 0
Share

Thank you very much for the answer, Sergio and Bunny83. Actually, I'm trying to move code monotone chain convex hull from C++ to the C# unity, and H[k++] is work in C++. And in C++ the inisialize array of H have size [2*n] . But, I read that size of list in C# is can't decided in inisialize. How can I repair this ? Any suggest ?

And sorry if my english is bad >< . I'm not english anymore

avatar image Sergio7888 samyn26 · Nov 13, 2016 at 03:58 PM 0
Share

@samyn26 C# list can only use index less than List Count, if you want use a index greater than count you need use a array, but using a array you will need specify the array size when you create the array, and you will need check if the index is less than array Length before set the value to index, if the index is equal or greater than Length you can use Array.Resize to change array Length.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by samyn26 · Nov 11, 2016 at 08:33 AM

Thank you very much for the answer, Sergio and Bunny83. Actually, I'm trying to move code monotone chain convex hull from C++ to the C# unity, and H[k++] is work in C++. And in C++ the inisialize array of H have size [2*n] . But, I read that size of list in C# is can't decided in inisialize. How can I repair this ? Any suggest ?

And sorry if my english is bad >< . I'm not english anymore

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

List Works But Still Get Argument Out of Range Exception 1 Answer

Why does List .ToArray throw ArgumentException? 1 Answer

JSON deserialization 0 Answers

Function is being called even when not told to 2 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