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?
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);
}
}
Thank you for the reply, but it didn't work. Still, every tempListListInt entry is the same :(
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());
}
}
@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.
Just checked it, it works! Thank you very much. Lovely.