• 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
Question by Ankhe · Sep 27, 2016 at 08:27 PM · c#memoryfreezelists

Unity freezes when I juggle Lists and SortedLists.

Hey everyone,

I have a scene full of objects - "Sites". Their positions are stored in a List < Vertex3 > siteList. I'd like to get a List < List < int > > siteNeighboursList - list of lists of neighbours - indexes of sites sorted by distance from each site.

My current code freezes Unity whenever I try to populate siteList with more than ~200 entries.

 private void CreateNeighboursList()
     {
         var oneSiteDistancesList = new SortedList(); // key - distance; value - site index
         var oneSiteSortedList = new List<int>(); //value - site index, sorted by distance
         float tempDistance;
 
         for (int i = 0; i < siteList.Count; i++)
         {
             oneSiteDistancesList.Clear();
             oneSiteSortedList.Clear();
 
             for (int j = 0; j < siteList.Count; j++)
             {
                 if (i != j) // to avoid sites trying to list themselves as neighbours
                 {
                     tempDistance = Vector3.Distance(siteList[i], siteList[j]);
                     w$$anonymous$$le (oneSiteDistancesList.ContainsKey(tempDistance)) // to avoid duplicate keys
                     {
                         tempDistance += 0.000001f;
                     }
                     oneSiteDistancesList.Add(tempDistance, j);
                 }
             }
             
             for (int j = 0; j < oneSiteDistancesList.Count; j++)
             {
                 oneSiteSortedList.Add((int)oneSiteDistancesList.GetByIndex(j));
             }
 
             siteNeighbours.Add(oneSiteSortedList);
         }
     }

Is there somet$$anonymous$$ng wrong with the code? Should I try different approach? Should I store the values differently?

Thanks in advance.

Comment

People who like this

0 Show 1
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 TBruce · Sep 27, 2016 at 08:52 PM 0
Share

Can I see exactly how you have siteNeighboursList defined?

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by TBruce · Sep 27, 2016 at 09:27 PM

It is most likely that as you add more and more items to the oneSiteDistancesList, it eventually takes longer to go through it. W$$anonymous$$ch make the w$$anonymous$$le loop appear to be endless. Try using a coroutine like t$$anonymous$$s instead

 List<Vertex3> siteList = new List<Vertex3>(); // assuming t$$anonymous$$s is how you have t$$anonymous$$s defined
 
 // moved the following two to be global
 private SortedList oneSiteDistancesList = new SortedList(); // key - distance; value - site index
 private List<int> oneSiteSortedList = new List<int>(); //value - site index, sorted by distance
 
 private void CreateNeighboursList()
 {
 
     if (siteList.Count > 0)
     {
         float tempDistance;
 
         for (int i = 0; i < siteList.Count; i++)
         {
             oneSiteDistancesList.Clear();
             oneSiteSortedList.Clear();
 
             for (int j = 0; j < siteList.Count; j++)
             {
                 if (i != j) // to avoid sites trying to list themselves as neighbours
                 {
                     tempDistance = Vector3.Distance(siteList[i], siteList[j]);
                     StartCoroutine(CheckDistance(tempDistance, j));
                 }
             }
 
             for (int j = 0; j < oneSiteDistancesList.Count; j++)
             {
                 oneSiteSortedList.Add((int)oneSiteDistancesList.GetByIndex(j));
             }
 
             siteNeighbours.Add(oneSiteSortedList);
         }
     }
 }
 
 IEnumerator CheckDistance(float tempDistance, int site)
 {
     w$$anonymous$$le (oneSiteDistancesList.ContainsKey(tempDistance)) // to avoid duplicate keys
     {
         tempDistance += 0.000001f;
         yield return null;
     }
     yield return null;
     oneSiteDistancesList.Add(tempDistance, site);
 }

It may still take a w$$anonymous$$le to process a large number of sites, but now other processing can take place including quitting the game immediately if desired.

Comment
Ankhe

People who like this

1 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 Ankhe · Sep 28, 2016 at 05:31 PM 0
Share

Edit: it seems that additions to siteNeighbours aren't being handled properly.

Wow, Coroutine helped! I haven't used them before. Here's how the code looks now:

  private List<Vector3> siteList = new List<Vector3>();
         private List<List<int>> siteNeighbours = new List<List<int>>();
         private SortedList oneSiteDistancesList = new SortedList(); // key - distance; value - site index
         private List<int> oneSiteSortedList = new List<int>(); //value - site index, sorted by distance
     
     private void CreateNeighboursList()
         {
             if (siteNeighbours.Count != siteList.Count)
             {
                 StartCoroutine(CreateNeighboursCoroutine());
             }
         }
     
     IEnumerator CreateNeighboursCoroutine()
         {
             if (siteList.Count > 0)
             {
                 float tempDistance;
                 for (int i = 0; i<siteList.Count; i++)
                 {
                     
                     oneSiteDistancesList.Clear();
                     oneSiteSortedList.Clear();
                     Debug.Log("CreateNeighboursCoroutine: i: " + i);
                     yield return null;
                     for (int j=0; j<siteList.Count; j++)
                     {
                         if (i != j) // to avoid sites trying to list themselves as neighbours
                         {
                             
                             tempDistance = Vector3.Distance(siteList[i], siteList[j]);
                             while (oneSiteDistancesList.ContainsKey(tempDistance)) // to avoid duplicate keys
                             {
                                 tempDistance += UnityEngine.Random.Range(0f,0.01f);
                                 yield return null;
                             }
                             oneSiteDistancesList.Add(tempDistance, j);
                         }
                     }
                     for (int j = 0; j < oneSiteDistancesList.Count; j++)
                     {
                         oneSiteSortedList.Add((int)oneSiteDistancesList.GetByIndex(j));
                     }
                         siteNeighbours.Add(oneSiteSortedList);
                 }
             }
         }

Thanks again!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

242 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 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 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

List give a count of zero when it have 1 element 1 Answer

"error CS1501: No overload for method `Add' takes `2' arguments" 0 Answers

a list with a constructor class that also contains a list. 1 Answer

Compare GameObject with an array 1 Answer

Fill new array with an old array every time two agents collide 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