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 28, 2016 at 06:56 PM · c#listcoroutinelist of lists

Adding to a List inside a Coroutine doesn't add properly?

Hey everyone, I have a piece of code that can be simplified to this:

 private List<List<int>> tempListListInt = new List<List<int>>();
     private List<int> tempListInt = new List<int>();
 
 void Start()
     {
         StartCoroutine(TestListListInt());
     }
 
 IEnumerator TestListListInt()
     {
         int tempInt;
         for (int i = 0; i < 10; i++)
         {
             tempListInt.Clear();
             yield return null;
             for (int j = 0; j < 10; j++)
             {
                 tempInt = Random.Range(0, 10);
                 tempListInt.Add(tempInt);
             }
             tempListListInt.Add(tempListInt);
         }
     }

when reading tempListListInt each List < int > stored inside is a copy of a List < int > tempListInt generated for i=9. I suppose I screwed something up with accessing global tempListListInt, but I am at loss as to what exactly. Help, pretty please?

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 TBruce · Sep 28, 2016 at 08:06 PM

Instead of List<List<int>> try using a class, like this

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 [System.Serializable]
 public class IntList
 {
     public List<int> list = new List<int>();
 }
  
 public class  : MonoBehaviour
 {
     private List<IntList> tempListListInt = new List<IntList>();
     private IntList tempListInt = new IntList();
 
     void Start()
     {
         StartCoroutine(TestListListInt());
     }
 
     IEnumerator TestListListInt()
     {
         int tempInt;
         int total = 0;
         tempListListInt.Clear();
         for (int i = 0; i < 10; i++)
         {
             tempListInt.Clear();
             yield return null;
             for (int j = 0; j < 10; j++)
             {
                 tempInt = Random.Range(0, 10);
                 total += tempInt;
                 tempListInt.list.Add(tempInt);
             }
             tempListListInt.Add(tempListInt);
         }
         yield return null;
         ShowListTotal(total);
     }
 
     // shows the total calculated in coroutine
     // and total from list
     void ShowListTotal(int total)
     {
         int tempTotal = 0;
         for (i = 0; i < tempListListInt.Count; i++)
         {
             for (j = 0; j < tempListListInt[i].list.Count; j++)
             {
                 tempTotal += tempListListInt[i].list[j];
             }
         }
         Debug.Log("Coroutine total equals " + total + ", list total equals " + tempTotal);
     }
 }
Comment
Add comment · Show 4 · 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 09:24 PM 0
Share

Thank you for the reply, but it didn't work. Still, every tempListListInt entry is the same :(

avatar image TBruce Ankhe · Sep 28, 2016 at 10:38 PM 1
Share

I did not test my original code. The problem was the reuse of the following private property

 private IntList tempListInt = new IntList();

This is the reason why each iteration of tempListListInt was always the same (they always stored the last value). Way was required was to replace this

 tempListInt.Clear();

with this

 IntList tempListInt = new IntList();

so that when adding to the tempListListInt each value would be unique. See the updated script below

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 [System.Serializable]
 public class IntList
 {
     public List<int> list = new List<int>();
 }
  
 public class TestTempList : $$anonymous$$onoBehaviour
 {
     private List<IntList> tempListListInt = new List<IntList>();
 
     void Start()
     {
         Calculate();
     }
     
     IEnumerator TestListListInt()
     {
         int tempInt;
         int total = 0;
         tempListListInt.Clear();
         for (int i = 0; i < 10; i++)
         {
             IntList tempListInt = new IntList();
             yield return null;
             for (int j = 0; j < 10; j++)
             {
                 tempInt = Random.Range(0, 10);
                 total += tempInt;
                 tempListInt.list.Add(tempInt);
             }
             tempListListInt.Add(tempListInt);
         }
         yield return null;
         ShowListTotal(total);
     }
 
     // shows the total calculated in coroutine
     // and total from list
     void ShowListTotal(int total)
     {
         int tempTotal = 0;
         for (int i = 0; i < tempListListInt.Count; i++)
         {
             for (int j = 0; j < tempListListInt[i].list.Count; j++)
             {
                 tempTotal += tempListListInt[i].list[j];
             }
         }
         Debug.Log("Coroutine total equals " + total + ", list total equals " + tempTotal);
     }
 
     public void Calculate()
     {
         StartCoroutine(TestListListInt());
     }
 }
avatar image TBruce Ankhe · Sep 29, 2016 at 02:17 AM 0
Share

@Ankhe Have you had any success with this? Here is a link to a Unity package that tests the functionality of the list.

The demo has a button so that allows continual test as well as a text field to show results.

avatar image Ankhe TBruce · Sep 29, 2016 at 04:36 PM 0
Share

Just checked it, it works! Thank you very much. Lovely.

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

245 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 avatar image avatar image avatar image

Related Questions

Call a coroutine from a list multiple times 0 Answers

Most efficient way to store information in an inspector dropdown menu? 0 Answers

Using a specific function from a generic object. 0 Answers

check if a list contains the same id as in another list 1 Answer

Array with pushing values? 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