问题遇到的现象和发生背景
在Unity中使用C#编程时,为什么不能将数据从一个数组存至另一个数组
问题相关代码,请勿粘贴截图
pointX和dataArrX都是字符串数组string[]
pointX[i] = dataArrX[i]; //运行到这里报错
运行结果及报错内容
NullReferenceException: Object reference not set to an instance of an object
在Unity中使用C#编程时,为什么不能将数据从一个数组存至另一个数组
pointX和dataArrX都是字符串数组string[]
pointX[i] = dataArrX[i]; //运行到这里报错
NullReferenceException: Object reference not set to an instance of an object
你应该是没有给point[i]指定长度吧!
在for循环之前初始化pointX的容量就行
pointX = new string[dataArrX.Length];
或者你可以用List来存储字符串,直接add,动态分配,就不会有这个问题了