Question by
strikeric11 · Nov 05, 2016 at 12:17 PM ·
unity5generic list
list.Count problem
My code creates a list that eliminates same triple or higher value.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class list : MonoBehaviour
{
List<int> onlydouble = new List<int>();
int listLength;
int checkdouble;
int randno;
void Start()
{
listLength = 7;
Gener8only2dupli();
}
void Gener8only2dupli()
{
while (onlydouble.Count != listLength)
{
Debug.Log(onlydouble.Count);
randno = Random.Range(0, 3);
Debug.Log("randno" + randno);
//Debug.Log("onlydouble" + onlydouble.Count);
//for (int j = 0; j < MaxNumbers; j++)
checkdouble = 0;
foreach (int value in onlydouble)
{
//Debug.Log("a" + a + "j" + j);
if (randno == value)
{
Debug.Log("incremented");
checkdouble +=1;
}
}
if (checkdouble > 2)
{
Debug.Log(randno + "greater than two");
}
else
{
onlydouble.Add(randno);
Debug.Log("added" + randno);
}
}
}
}
My problem is that when Debug.Log(onlydouble.Count); executes it says it has a new element added even thought it did not add a new element. What's the proper way in checking the number of elements in generic list in Unity? Thanks
Comment
No different than regular C#. dot-Count is fine. There's probably just a logic mistake in your code.
The usual next debugging thing would be to print all the values.
Answer by strikeric11 · Nov 05, 2016 at 04:06 PM
I don't know what happened but when I run it again it runs smoothly. weird.
Your answer
![](https://koobas.hobune.stream/wayback/20220612100105im_/https://answers.unity.com/themes/thub/images/avi.jpg)