出现了这样的错误
IndexOutOfRangeException: Index was outside the bounds of the array.
什么问题导致的呢,解决的思路请详细说明
for (int i = 0; i <= 32; i++)
{
float x = float.Parse(points[0 + (i * 3)]) / 100;
float y = float.Parse(points[1 + (i * 3)]) / 100;
float z = float.Parse(points[2 + (i * 3)]) / 100;
Body[i].transform.localPosition = new Vector3 (x,y,z);
for(int i=0;i<32;i++)
应该不能等于32的。一般要特别注意循环结束条件
下标越界了
这是完整代码
```c#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using System.Threading;
public class AnimationCode : MonoBehaviour
{
public GameObject[] Body;
List<string> lines;
int counter = 0;
// Start is called before the first frame update
void Start()
{
lines = System.IO.File .ReadLines("Assets/data1.txt").ToList();
}
// Update is called once per frame
void Update()
{
string[] points = lines[counter].Split(',');
for (int i = 0; i <=32; i++)
{
float x = float.Parse(points[0 + (i * 3)]) / 100;
float y = float.Parse(points[1 + (i * 3)]) / 100;
float z = float.Parse(points[2 + (i * 3)]) / 300;
Body[i].transform.localPosition = new Vector3 (x,y,z);
// Debug.Log(Body.Length);
}
counter += 1;
if (counter == lines.Count) { counter = 0; }
Thread.Sleep(30);
}
}
```@