I just encountered some strange behavior. I am creating a string[], each index containing 16x '0'. Then I want to change that char to a different type as a spawning list. I run the script. And got an nullpointer exception. So I started debugging. I do not understand how this is happening: Note: I switched to .net 4.5 in Unity, is this causing it? Code (CSharp): spawnList = new string[val.level * 2 + 10]; for (int i = 0; i < spawnList.Length; i++) { for (int j = 0; j < 16; j++) { spawnList[i] += "0"; } } char[] tmp; int badLuck = 10; int count = 1; while (maxCoins > 0) { int start = Random.Range(1, 4); int step = Random.Range(1, 4); for (int i = start; i < val.level * 2 + 10; i += step) { for (int j = 0; j < 16; j++) { Debug.Log(i + " " + j + " " + spawnList[i] + " " + spawnList[i][j]); [......] It created this output: I also tryed Debug.Log(i + " " + j + " " + spawnList[i] + " " + spawnList[i][j]); Debug.Log(i + " " + j + " " + spawnList[i] + " " + spawnList[i].toCharArray()[j]); Debug.Log(i + " " + j + " " + spawnList[i].toString() + " " + spawnList[i].toCharArray()[j].toString());
Yea... I just discoverd that a char[] var with .toString() will in fact return System.Char[] and not the char[] as string... Had: Code (CSharp): char[] tmp = spawnList[i].ToCharArray(); tmp[j] = 'g'; spawnList[i] = tmp.toString(); Correct: Code (CSharp): char[] tmp = spawnList[i].ToCharArray(); tmp[j] = 'g'; spawnList[i] = new string(tmp);
Code (csharp): char[] bar = new char[10]; var baz = bar.ToString(); Debug.Log(baz.GetType()); prints out System.String.