ページ 11

UnityでArray index is out of rangeエラーが出ます

Posted: 2016年11月19日(土) 17:05
by Mokutsuno
いつもお世話になっています。
現在、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 = component.GetType().Name;でエラーが発生しているようです。
そしてこのエラーは配列のサイズを超えてアクセスしようとした場合に出るエラーだと思うのですが、CompNameは要素数は指定せずに宣言しているので、サイズをこえることは無いと思うのですが
何か勘違いしているのだと思います。
どうコードを記述すればエラーを回避できるか教えていただきたいです.

Re: UnityでArray index is out of rangeエラーが出ます

Posted: 2016年11月19日(土) 18:56
by あんどーなつ
CompName = comp.GetType().Name;
の前に
CompName = new string[comp.Length];
と書いてください。

Re: UnityでArray index is out of rangeエラーが出ます

Posted: 2016年11月19日(土) 18:57
by あんどーなつ
すいません。19行目と20行目の間です。

Re: UnityでArray index is out of rangeエラーが出ます

Posted: 2016年11月19日(土) 19:07
by あんどーなつ
ちょっと気になったのですが、

コード:

            CompName[i] = comp.GetType().Name;
            Debug.Log(CompName[i]);
            i++;

コード:

            Debug.Log(components.GetType().Name);
でいいと思います。

Re: UnityでArray index is out of rangeエラーが出ます

Posted: 2016年11月19日(土) 19:27
by Mokutsuno
ありがとうございます。動きました。
あんどーなつ さんが書きました:ちょっと気になったのですが、

コード:

            CompName[i] = comp.GetType().Name;
            Debug.Log(CompName[i]);
            i++;

コード:

            Debug.Log(components.GetType().Name);
でいいと思います。
確かに、無駄なことをしていました。書き換えます。指摘ありがとうございます。

CompName = new string[comp.Length];の意味としては、
Component[] comp = GetComponents<Component>();でそれぞれComponentを参照して、comp.Length(コンポーネントの数)がわかったので、
CompName = new string[comp.Length];により、配列の長さをcomp.Lengthとしてインスタンスを生成する。
と言うことであっていますでしょうか?

Re: UnityでArray index is out of rangeエラーが出ます

Posted: 2016年11月19日(土) 20:33
by あんどーなつ
Mokutsuno さんが書きました: CompName = new string[comp.Length];の意味としては、
Component[] comp = GetComponents<Component>();でそれぞれComponentを参照して、comp.Length(コンポーネントの数)がわかったので、
CompName = new string[comp.Length];により、配列の長さをcomp.Lengthとしてインスタンスを生成する。
と言うことであっていますでしょうか?
Unityのドキュメントで、
GetComponent<Component>は、Component型のゲームオブジェクトでアタッチされているものがあればそれを返す、なければnull
GetComponents<Component>は、GameObject内のComponent型のコンポーネントをすべて返す
となっていたと思います(訳間違ってたら勘弁)。

また、Unityのドキュメントの中でComponent型はGameObjectにアタッチされているクラス全ての基本クラスとあるので、GetComponents<Component>は全てのゲームオブジェクトを取得するものと思います。

分かりづらいのですが、Component型とは別にComponent[]型というのがあります。GetComponentsメソッドは、「最初から用意されているcompという配列にそれぞれのComponent型オブジェクトの参照を格納」しているのではなく、「Component型オブジェクトの参照が格納されたComponent[]型オブジェクトを生成して、その参照をcompに格納」しています。

なので、GetComponentsメソッドが実行されるまでは、compはnullであり、comp.Lengthを呼び出すと例外が発生します。

Lengthがわかったからというよりは、compに配列オブジェクトの参照が格納されたので、という方が正しいです。

Re: UnityでArray index is out of rangeエラーが出ます

Posted: 2016年11月19日(土) 20:35
by あんどーなつ
書き忘れていました。
Mokutsuno さんが書きました:CompName = new string[comp.Length];により、配列の長さをcomp.Lengthとしてインスタンスを生成する。
は、合っています。

Re: UnityでArray index is out of rangeエラーが出ます

Posted: 2016年11月19日(土) 21:21
by Mokutsuno
よくわかりました。
ありがとうございました。