创建具有生成id的for循环的怪物

Dan*_*mta 1 c# for-loop unity-game-engine

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

好的,我们去,我有一个C#脚本,我想用于怪物生成.我在Youtube上关注了Tom Adamson,直到他开始产生随机值:UNITY3D C#和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();
        }

    }

}
Run Code Online (Sandbox Code Playgroud)

当我只有2个怪物时,这个工作正常,但当我尝试添加第三个怪物时,我得到了这个错误:

IndexOutOfRangeException:数组索引超出范围.(wrapper stelemref)对象:stelemref(object,intptr,object)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++;
Run Code Online (Sandbox Code Playgroud)

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

任何帮助深表感谢.

Kam*_*ski 8

aMonster[] bigMonster = new aMonster[51];
Run Code Online (Sandbox Code Playgroud)

意味着你有51个怪物(最大指数= 50),但你在一个for循环中递增两次,在最后一次迭代时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;

    }
Run Code Online (Sandbox Code Playgroud)

应该在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
}
Run Code Online (Sandbox Code Playgroud)

它会创造30种各种怪物,当然你可以通过modyfing for循环改变这个数字