我正在编写一个Unity脚本,该脚本读取CSV文件(2D和所有数字),并将其分解为附加到2D浮点数组的浮点数。这是我的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LoadCalibration : MonoBehaviour
{
public float[,] pc_array; // Reconstructed PC coefficient MD array (PCs as rows and variables as columns)
// Start is called before the first frame update
void Start()
{
// PC COEFFICIENTS
pc_array = new float[20, 20];
Debug.Log(pc_array);
TextAsset pc_data = Resources.Load<TextAsset>("pc_coeff"); //Data is in as variables x PCs
string[] variable = pc_data.text.Split(new char[] { '\n' }); // split pc_data into rows(each row is one variable, for all PCs)
for (int i = 0; i < variable.Length - 1; i++)
{
string[] pc = variable[i].Split(new char[] { ',' }); // delegate each variable to a pc
Debug.Log(i);
for (int j = 0; j < pc.Length; i++)
{
Debug.Log(j);
pc_array[j,i] = float.Parse(pc[j]); // Load float value into the pc_coeff MD array
}
}
}
}
它引发了我这个错误:
IndexOutOfRangeException: Index was outside the bounds of the array.
LoadCalibration.Start () (at Assets/Scripts/LoadCalibration.cs:31)
使用Debug.Log()
,我算出该错误发生在i = 0和j = 0(数组的第一个索引)处,即使我将其声明为20 x 20数组也是如此。我是C#的新手,所以错误很明显,但我无法解决。任何帮助将非常感激!
我已经使用Debug.Log()
来评估其余代码是否正常工作(也就是读取CSV文件并将每个字符串条目转换为单个浮点数)。
答案 0 :(得分:4)
for (int j = 0; j < pc.Length; i++)
更改为
for (int j = 0; j < pc.Length; j++)