I was trying to make an int list to store my values, so I defined it outside of any method and then wanted to set its length inside the start method, but I got an error (defined in the title of the thread). Here’s my code …
public class Player : MonoBehaviour {
private List<int>[] possibleValues;
private string currentMode = "";
private string[] playModes = {"evenToOdd", "oddToEven", "incremental"};
public Text chosenValues;
// Use this for initialization
public GameObject prefab;
void Start() {
possibleValues = new List<int>[2];
for (int x = 0; x < Random.Range (6,10); x++) {
GameObject go = Instantiate(prefab, new Vector3(Random.Range (-4,4), 1, Random.Range (-4,4)), Quaternion.identity) as GameObject;
Clickable clicky = go.GetComponent<Clickable>();
clicky.initiate(this, Random.Range (0,10));
}
}
Using StringBuilder from System.Text, you could do something like this considering your list (possibleValues) contains integers:
System.Text.StringBuilder sb = new System.Text.StringBuilder();
// IEnumerable iteration using bool to check if we're on first value
bool firstValue = true;
foreach(int _int in possibleValues)
{
if(firstValue)
{
sb.Append(_int);
firstValue = false;
}
else
{
sb.Append("," + _int);
}
}
// For loop grabbing values by index
for(int i = 0; i < possibleValues.Count; i++)
{
if(i == 0)
{
sb.Append(possibleValues[i]);
}
else
{
sb.Append("," + possibleValues[i]);
}
}
Debug.Log("Final string: " + sb.ToString());
Either one should work. Applying StringBuilder here because I read an article recently that appending strings manually can cause some excess work for the GC.
edit: I think I read something about IEnumerable iterations as well, can’t remember what exactly. If anybody reading this knows maybe clear it up for me, are IEnumerable iterations hefty towards memory management?
GC being “garbage collection”, the process that unity does to clean up the mess you most likely never realise your making behind the scenes until your project grinds to a halt for “no reason”