0

これが別の場所で回答されている場合はお詫び申し上げますが、私の問題は非常に具体的であり、python での過去の経験に基づいて、このコードがエラーを返す理由が少しもわかりませんでした。

IndexOutOfRangeException: Array index is out of range.
(wrapper stelemref) object:stelemref (object,intptr,object)
MissionGen.Start () (at Assets/Scripts/MissionGen.cs:59)

私は現在、Unity エンジンで C# を学習中であり、OOP を使用してミッションを生成し、配列からターゲット名を選択するように設計された小さなクラスの実験スクリプトを作成しましたが、明らかに、なぜこれが機能しないのかわかりません。Python で同等のエラーが発生するのは、存在しない配列要素を参照しようとした場合のみだと思います。これはおそらくここでも当てはまりますが、コードの書き方のせいで、その理由がわかりません。とにかく、ここに完全なスクリプトがあります!:

// Simple mission generation script, using arrays and object orientated programming. Designed to be
//  upgraded at a later date. 

using UnityEngine;
using System.Collections;

// Mission generation template

public class MissionGen : MonoBehaviour {

    public string[] t_list_t = new string[] {"Dave", "Johnson", "Hadaki", "Tim", "Timothy", "Chris Roberts"};
    public string[] t_list_c_col = new string[] {"Blue", "Yellow", "Green", "Black", "Orange", "Purple"};
    public string[] t_list_h_col = new string[] {"Black", "Green", "Orange", "Blue", "Red", "Brown"};

    public class MissionTemplate {

    // Mission properties

        public int id;
        public string t_name;
        public string t_coat;
        public string t_hat;
        public string type;
        public int reward1;
        public string reward2;

    // A method, for displaying the attributes of an individual mission.

    public void ShowMission () {
        print ("MISSION ID: " + id);
        print ("TARGET NAME: " + t_name);
        print ("TARGET COAT COLOUR: " + t_coat);
        print ("TARGET HAT COLOUR: " + t_hat);
        print ("MISSION TYPE: " + type);
        print ("REWARD 1 (Money): " + reward1);
        print ("REWARD 2 (Item): " + reward2);
    }
}

// Mission array generation. Change the array range to generate more missions for use in the game.

    void Start() {

        // Change this variable to decide how many missions to generate:

        int gen = 50;

        // Change the above variable to designate the number of missions to generate.

        MissionTemplate[] MISSION = new MissionTemplate[gen];

        for (int i = 1; i <= gen; i++)
        {   

        int t_pick = Random.Range (0,5);
        int t_coat_col = Random.Range (0,5);
        int t_hat_col = Random.Range (0,5);

        MISSION[i] = new MissionTemplate();

        MISSION[i].id = i;
        MISSION[i].t_name = t_list_t[t_pick];
        MISSION[i].t_coat = t_list_c_col[t_coat_col];
        MISSION[i].t_hat = t_list_h_col[t_hat_col];
        MISSION[i].type = "Assassination";
        MISSION[i].reward1 = 0;
        MISSION[i].reward2 = "";
        }

        for (int i = 1; i <= gen; i++)
        {
        MISSION[i].ShowMission();   
        }
    }
}

内訳として、次のコード間で問題が発生すると思います。

    for (int i = 1; i <= gen; i++)
    {   

    int t_pick = Random.Range (0,5);
    int t_coat_col = Random.Range (0,5);
    int t_hat_col = Random.Range (0,5);

    MISSION[i] = new MissionTemplate();

    MISSION[i].id = i;
    MISSION[i].t_name = t_list_t[t_pick];
    MISSION[i].t_coat = t_list_c_col[t_coat_col];
    MISSION[i].t_hat = t_list_h_col[t_hat_col];
    MISSION[i].type = "Assassination";
    MISSION[i].reward1 = 0;
    MISSION[i].reward2 = "";
    }

    for (int i = 1; i <= gen; i++)
    {
    MISSION[i].ShowMission();   
    }
}

}

と:

public class MissionGen : MonoBehaviour {

    public string[] t_list_t = new string[] {"Dave", "Johnson", "Hadaki", "Tim", "Timothy", "Chris Roberts"};
    public string[] t_list_c_col = new string[] {"Blue", "Yellow", "Green", "Black", "Orange", "Purple"};
    public string[] t_list_h_col = new string[] {"Black", "Green", "Orange", "Blue", "Red", "Brown"};

どんな助けでも大歓迎です!

ありがとう、

4

1 に答える 1