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

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
Mokutsuno
記事: 26
登録日時: 8年前

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

#1

投稿記事 by Mokutsuno » 8年前

いつもお世話になっています。
現在、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は要素数は指定せずに宣言しているので、サイズをこえることは無いと思うのですが
何か勘違いしているのだと思います。
どうコードを記述すればエラーを回避できるか教えていただきたいです.

あんどーなつ
記事: 171
登録日時: 8年前
連絡を取る:

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

#2

投稿記事 by あんどーなつ » 8年前

CompName = comp.GetType().Name;
の前に
CompName = new string[comp.Length];
と書いてください。


あんどーなつ
記事: 171
登録日時: 8年前
連絡を取る:

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

#4

投稿記事 by あんどーなつ » 8年前

ちょっと気になったのですが、

コード:

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

コード:

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

Mokutsuno
記事: 26
登録日時: 8年前

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

#5

投稿記事 by Mokutsuno » 8年前

ありがとうございます。動きました。
あんどーなつ さんが書きました:ちょっと気になったのですが、

コード:

            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としてインスタンスを生成する。
と言うことであっていますでしょうか?

あんどーなつ
記事: 171
登録日時: 8年前
連絡を取る:

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

#6

投稿記事 by あんどーなつ » 8年前

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に配列オブジェクトの参照が格納されたので、という方が正しいです。

あんどーなつ
記事: 171
登録日時: 8年前
連絡を取る:

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

#7

投稿記事 by あんどーなつ » 8年前

書き忘れていました。
Mokutsuno さんが書きました:CompName = new string[comp.Length];により、配列の長さをcomp.Lengthとしてインスタンスを生成する。
は、合っています。

Mokutsuno
記事: 26
登録日時: 8年前

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

#8

投稿記事 by Mokutsuno » 8年前

よくわかりました。
ありがとうございました。

閉鎖

“C言語何でも質問掲示板” へ戻る