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 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]);
                     while (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 something wrong with the code? Should I try different approach? Should I store the values differently?

Thanks in advance.

Comment
Add comment · 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
1
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. Which make the while loop appear to be endless. Try using a coroutine like this instead

 List<Vertex3> siteList = new List<Vertex3>(); // assuming this is how you have this 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)
 {
     while (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 while to process a large number of sites, but now other processing can take place including quitting the game immediately if desired.

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 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.Contains$$anonymous$$ey(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.

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

Unity game freezes a few seconds after loading scene 2 Answers

Trying to program two buttons to appear when the player in my game dies 0 Answers

How to approach my management simulation game? 0 Answers

C# - Two lists linked with the same value 1 Answer

How do I make my A* script work with prefab enemies and make enemies move using the list created through A*? 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