現在、Unityでスクリプトを書いているのですが、IndexOutOfRangeException: Array index is out of range.
と、エラーが出てしまいます。
using UnityEngine;
using System.Collections;
public class Sample : MonoBehaviour
{
//SerializedProperty anchorProperty;
public string[] CompName;
public int VertexNum = 0;
public int VertexNumMax = 0;
public Color gizmoColor = Color.red;
public float SphereScale = 0.001f;
public bool toggle = false;
public Vector3[] vertices;
public int arraynum;
//private string vertexPosition = null;
public void GetComp()
{
int i = 0;
Component[] comp = GetComponents<Component>();
foreach (Component components in comp)
{
// Debug.Log("a");
//Debug.Log(components.GetType().Name);
CompName[i] = comp.GetType().Name;
Debug.Log(CompName[i]);
i++;
}
}
そしてこのエラーは配列のサイズを超えてアクセスしようとした場合に出るエラーだと思うのですが、CompNameは要素数は指定せずに宣言しているので、サイズをこえることは無いと思うのですが
何か勘違いしているのだと思います。
どうコードを記述すればエラーを回避できるか教えていただきたいです.