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 g__l · Mar 05, 2016 at 04:44 PM · errorinstantiatelistif statementwhile loop

Delete duplicates in a list of vectors

Pretty much as the title says. In the inspector i can see many objects with the same position. I have created some spheres, i then add them to a list, destroy all of them, then get rid of the duplicates in the list then instantiate them back into the game. The problem for me is getting rid of duplicates.

What i have tried and done - Used while statements and if statement to scan through and delete duplicates (in between the here comments) - Used .distinct().tolist() as suggested in other suggestions which did not work as proven by the 2 debug statements as shown - Made sure that the values are all rounded to the same degree of accuracy (3 d.p) which worked as shown in the inspector but it did not help me get rid of duplicates

My code is shown below with important parts mentioned above.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 
 public class SphereDeleteRepeat : MonoBehaviour {
     List<GameObject> Spheres;
     GameObject[] gameObjects;
     Rect area;
 
     // Use this for initialization
     void DoAble () {
         gameObjects = GameObject.FindGameObjectsWithTag("Sphere");
         Debug.Log(gameObjects.Length);
         Spheres = new List<GameObject>();
         foreach (GameObject sph in GameObject.FindGameObjectsWithTag("Sphere"))
         {
             Spheres.Add(sph);
         }
         Debug.Log("First " + Spheres.Count); // 125
         Spheres = Spheres.Distinct().ToList();
         Debug.Log("Last " + Spheres.Count); // 125
         int i = 0;
         while (i < gameObjects.Length)
         {
             Destroy(gameObjects[i]);
             i = i + 1;
         }
         // HERE
         i = 0;
         while (i < Spheres.Count)
         {
             int i2 = 0;
             while (i2 < Spheres.Count)
             {
                 if (i2 != i && Spheres[i].transform.position == Spheres[i2].transform.position)
                 {
                     Spheres.RemoveAt(i2);
                 }
                 i2++;
             }
             i++;
         }
         // HERE
         Debug.Log(Spheres.Count);
         area.xMin = -2.6f;
         area.xMax = 2.6f;
         area.yMin = -3;
         area.yMax = 3;
         i = 0;
         while (i < Spheres.Count)
         {
             if (area.Contains(Spheres[i].transform.position) == true)
             {
                 Instantiate(Spheres[i], Spheres[i].transform.position, Quaternion.identity);
             }
             i++;
         }
     }
     
     // Update is called once per frame
     void Update () {
     if (Time.frameCount == 2)
         {
             DoAble();
         }
     }
 }

I get the error message "ArgumentOutOfRangeException: Argument is out of range. Parameter name: index" which dissapears when i remove the Spheres.RemoveAt line.

Thanks for any help it is much appreciated as this problem has been annoying me for ages.

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 Glurth · Mar 05, 2016 at 06:43 PM

 Spheres.RemoveAt(i2);
 i2--; //add this immediately below Remove()

You need to decrement your index, after you remove the element. Because you will increment again at the end of the loop, if you DON'T decrement, you will skip the item that immediately followed the one you deleted. (is that clear, at all?)

Hmm. not sure if you will need to do the same for the i index, used by the outerloop: but I would think SO.

 Spheres.RemoveAt(i2);
 i2--; //add this immediately below Remove()
 i--;


Note: Be careful, if you choose to optimize this later, and STORE the list size in a variable, rather than use list.Count in your loop's conditional- you will need to decrement THAT variable too, when you Remove() one.

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 g__l · Mar 06, 2016 at 11:12 AM 0
Share

Thank you very much, that works perfectly, i have tried both and i see no difference so i have stuck with the second.

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

47 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

Related Questions

Destroy GameObject in list then instantiate GameObject not working 1 Answer

My gun reloading dont work,can i please get help. 2 Answers

Script component seems to be missing when spawning prefab? 2 Answers

Why do I keep getting 'The object you want to instantiate is null' warning? 1 Answer

iTween NullReferenceException from GameObject Instantiation 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