1

我是游戏编程和 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:数组索引超出范围。(wrapper stelemref) object:stelemref (object,intptr,object) myMonster.Start () (at 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++;

谁能告诉我我做错了什么?我想 i++ 是错误的做法,因为第三个怪物导致了错误。

任何帮助深表感谢。

4

3 回答 3

8
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个怪物,当然你可以通过修改for循环来改变这个数字

于 2013-11-12T10:28:00.340 回答
0

从0开始迭代

for (int i = 1; i <= 50;) 
于 2013-11-12T10:34:25.283 回答
0

为了让事情变得更容易,我将使用 List 而不是数组。符号也会更容易,例如:

var bigMonster = new list<aMonster>();

for(int i = 1; i <=50; )
{
  var mon1 = new aMonster{id = i, name = "Gorky", race = "orc", age = 320, health = 200};
        i++;
  var mon2 = new aMonster{id = i, name = "Runathu", race = "Shaman", age = 670, health = 100};
        i++;
  var mon3 = new aMonster{id = i, name = "Tiny", race = "Spider", age = 90, health = 45};
        i++;
  bigMonster.Add(mon1);
  bigMonster.Add(mon2);
  bigMonster.Add(mon3);
}

foreach(aMonster a in bigMonster)
{
  a.monsterData();
}
于 2013-11-12T10:41:53.657 回答