我是游戏编程和C#的新手,但是我有一些JavaScript和PHP的编程经验。

好的,我们开始吧,我有一个C#脚本,我想将其用于生成怪物。我已经在YouTube上关注汤姆·亚当森(Tom Adamson),直到他开始生成此处显示的随机值为止:UNITY3D C# with Tom Adamson

这是我的脚本:

using UnityEngine;
using System.Collections;

public class myMonster : MonoBehaviour {

    public class aMonster {

        //The properties
        public int id;
        public int age;
        public string name;
        public string race;
        public int health;

        //A method
        public void monsterData() {
            print("ID: "        + id);
            print("Race: "      + race);
            print("Name: "      + name);
            print("Age: "       + age);
            print("Health: "    + health);
        }

    } // End of class definition

//-----------------------------------------------------------------------------------------

    void Start () {

        aMonster[] bigMonster = new aMonster[51];

        for (int i = 1; i <= 50;) {

            bigMonster[i] = new aMonster();
            bigMonster[i].id = i;
            bigMonster[i].name = "Gorky";
            bigMonster[i].race = "Orc";
            bigMonster[i].age = 320;
            bigMonster[i].health = 200;
            i++;

            bigMonster[i] = new aMonster();
            bigMonster[i].id = i;
            bigMonster[i].name = "Runathu";
            bigMonster[i].race = "Shaman";
            bigMonster[i].age = 670;
            bigMonster[i].health = 100;
            i++;

        }

        for (int i = 1; i <= 2; i++) {
            bigMonster[i].monsterData();
        }

    }

}


当我只有2个怪物时,此方法工作正常,但是当我尝试添加第三个怪物时,出现此错误:

IndexOutOfRangeException:数组索引超出范围。
(包装器stelemref)对象:stelemref(对象,intptr,对象)
myMonster.Start()(位于Assets / myMonster.cs:50)

我添加了第三个怪物,如下所示:

bigMonster[i] = new aMonster();
            bigMonster[i].id = i;
            bigMonster[i].name = "Gorky";
            bigMonster[i].race = "Orc";
            bigMonster[i].age = 320;
            bigMonster[i].health = 200;
            i++;

            bigMonster[i] = new aMonster();
            bigMonster[i].id = i;
            bigMonster[i].name = "Runathu";
            bigMonster[i].race = "Shaman";
            bigMonster[i].age = 670;
            bigMonster[i].health = 100;
            i++;

            bigMonster[i] = new aMonster();
            bigMonster[i].id = i;
            bigMonster[i].name = "Tiny";
            bigMonster[i].race = "Spider";
            bigMonster[i].age = 90;
            bigMonster[i].health = 45;
            i++;


谁能告诉我我在做什么错?我猜我是这样做的错误方法,因为第三个怪物引起了错误。

任何帮助深表感谢。

最佳答案

aMonster[] bigMonster = new aMonster[51];


表示您有51个怪兽(最大指数= 50),但您要在一次for循环中将i递增两次,最后一次迭代时i = 50,因此您尝试达到aMonster[51]

固定:

从i = 0开始循环,在i = 49结束,c#中的索引从0开始而不是1

我也建议您将代码转换为:

    for (int i = 0; i < 50; i+=2)
    {

        bigMonster[i] = new aMonster();
        bigMonster[i].id = i;
        bigMonster[i].name = "Gorky";
        bigMonster[i].race = "Orc";
        bigMonster[i].age = 320;
        bigMonster[i].health = 200;


        bigMonster[i+1] = new aMonster();
        bigMonster[i+1].id = i;
        bigMonster[i+1].name = "Runathu";
        bigMonster[i+1].race = "Shaman";
        bigMonster[i+1].age = 670;
        bigMonster[i+1].health = 100;

    }


for循环的增量应该在for循环的定义中完成,它看起来更干净。

编辑:

最优雅,最安全的解决方案,请使用List<aMonster>()

var bigMonster = new List<aMonster>();
var id = 0;
for(int i=0; i<30; i++)
{
    bigMonster.Add(new aMonster { id=id++,name="Gorky",race="Orc",age=320,health=200 });
    bigMonster.Add(new aMonster { id=id++,name="Runathu",race="Shaman",age=320,health=200 });
    //and so on
}


它会创建30种各种怪物,当然,您可以通过modyfing for loop更改此数字

10-04 21:38