我正在研究一个使用正方形的简单世界生成器。但它在随机数行后失败,它没有实例化最后一项。
我无法理解为什么它失败了,因为它有可用的块选项放在那个位置。在相同数量的行之后,它并不总是失败。
它有时也会将错误的块放在某些地方,但我认为这是由于另一个问题 错误:
ArgumentOutOfRangeException: Argument is out of range. Parameter name: index
System.Collections.Generic.List`1[UnityEngine.GameObject].get_Item (Int32 index
(at/Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:633) GenGround.Start () (at Assets/Code/GenGround.cs:93)
代码:
using UnityEngine;
using System.Collections;
using System.Linq;
using System.Collections.Generic;
public class GenGround: MonoBehaviour {
// Use this for initialization
public GameObject[] terrainTypes;
void Start() {
int x = 0;
int y = 0;
int i = 0;
GameObject[] itemList = new GameObject[10000];
terrainTypes = GameObject.FindGameObjectsWithTag("Terrain").OrderBy(go => go.name).ToArray();
List <GameObject> WaterDeepOpts = new List <GameObject> ();
WaterDeepOpts.Add(terrainTypes[3]);
WaterDeepOpts.Add(terrainTypes[4]);
List <GameObject> WaterShallowOpts = new List <GameObject> ();
WaterShallowOpts.Add(terrainTypes[3]);
WaterShallowOpts.Add(terrainTypes[4]);
WaterShallowOpts.Add(terrainTypes[1]);
List <GameObject> SandOpts = new List <GameObject> ();
SandOpts.Add(terrainTypes[0]);
SandOpts.Add(terrainTypes[1]);
SandOpts.Add(terrainTypes[4]);
List <GameObject> GrassOpts = new List <GameObject> ();
GrassOpts.Add(terrainTypes[0]);
GrassOpts.Add(terrainTypes[1]);
GrassOpts.Add(terrainTypes[2]);
List <GameObject> StoneOpts = new List <GameObject> ();
StoneOpts.Add(terrainTypes[0]);
StoneOpts.Add(terrainTypes[2]);
List <GameObject> Block1 = new List <GameObject> ();
List <GameObject> Block2 = new List <GameObject> ();
List <GameObject> Blocks = new List <GameObject> ();
foreach(GameObject g in terrainTypes) {
Debug.Log(g);
};
itemList[i] = Instantiate(terrainTypes[Random.Range(0, terrainTypes.Length)], new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
x = x + 10;
i = i + 1;
while (y < 1000) {
if (i < 100) {
//Debug.Log (i);
if (itemList[i - 1].name == "WaterDeep(Clone)") {
itemList[i] = Instantiate(WaterDeepOpts[Random.Range(0, WaterDeepOpts.Count())], new Vector3(x, 0, y), Quaternion.identity) as GameObject;
} else if (itemList[i - 1].name == "WaterShallow(Clone)") {
itemList[i] = Instantiate(WaterShallowOpts[Random.Range(0, WaterShallowOpts.Count())], new Vector3(x, 0, y), Quaternion.identity) as GameObject;
//Debug.Log (itemList [i]);
} else if (itemList[i - 1].name == "Sand(Clone)") {
itemList[i] = Instantiate(SandOpts[Random.Range(0, SandOpts.Count())], new Vector3(x, 0, y), Quaternion.identity) as GameObject;
} else if (itemList[i - 1].name == "Grass(Clone)") {
itemList[i] = Instantiate(GrassOpts[Random.Range(0, GrassOpts.Count())], new Vector3(x, 0, y), Quaternion.identity) as GameObject;
} else if (itemList[i - 1].name == "Stone(Clone)") {
itemList[i] = Instantiate(StoneOpts[Random.Range(0, StoneOpts.Count())], new Vector3(x, 0, y), Quaternion.identity) as GameObject;
} else {
itemList[i] = Instantiate(terrainTypes[Random.Range(0, terrainTypes.Length)], new Vector3(x, 0, y), Quaternion.identity) as GameObject;
}
} else if (y > 0 && x > 0) {
//Debug.Log(itemList[i - 1].name);
// Debug.Log(x);
if (itemList[i - 1].name == "WaterDeep(Clone)") {
Block1 = WaterDeepOpts;
} else if (itemList[i - 1].name == "WaterShallow(Clone)") {
Block1 = WaterShallowOpts;
} else if (itemList[i - 1].name == "Sand(Clone)") {
Block1 = SandOpts;
} else if (itemList[i - 1].name == "Grass(Clone)") {
Block1 = GrassOpts;
} else {
Block1 = StoneOpts;
}
if (itemList[i - 99].name == "WaterDeep(Clone)") {
Block2 = WaterDeepOpts;
} else if (itemList[i - 99].name == "WaterShallow(Clone)") {
Block2 = WaterShallowOpts;
} else if (itemList[i - 99].name == "Sand(Clone)") {
Block2 = SandOpts;
} else if (itemList[i - 99].name == "Grass(Clone)") {
Block2 = GrassOpts;
} else {
Block2 = StoneOpts;
}
Blocks = Block1.Intersect(Block2).ToList();
//Debug.Log(Blocks.Count());
int d = Random.Range(0, Blocks.Count());
// Debug.Log(d);
// foreach(GameObject g in Blocks) {
// Debug.Log(g);
// };
Debug.Log("Blocks = " + Blocks[d].name);
itemList[i] = Instantiate(Blocks[d], new Vector3(x, 0, y), Quaternion.identity) as GameObject;
} else if (y > 0 && x == 0) {
// Debug.Log(x);
if (itemList[i - 99].name == "WaterDeep(Clone)") {
// Debug.Log("BONG!");
Blocks = WaterDeepOpts;
} else if (itemList[i - 99].name == "WaterShallow(Clone)") {
// Debug.Log("BONG!");
Blocks = WaterShallowOpts;
} else if (itemList[i - 99].name == "Sand(Clone)") {
// Debug.Log("BONG!");
Blocks = SandOpts;
} else if (itemList[i - 99].name == "Grass(Clone)") {
// Debug.Log("BONG!");
Blocks = GrassOpts;
} else {
Blocks = StoneOpts;
// Debug.Log("BONG!");
}
//Blocks = Block1.Intersect(Block2).ToList();
// Debug.Log("BONG!");
int d = Random.Range(0, Blocks.Count());
Debug.Log(d);
Debug.Log("count=" + Blocks.Count());
itemList[i] = Instantiate(Blocks[d], new Vector3(x, 0, y), Quaternion.identity) as GameObject;
} else {
itemList[i] = Instantiate(terrainTypes[Random.Range(0, terrainTypes.Length)], new Vector3(x, 0, y), Quaternion.identity) as GameObject;
}
x = x + 10;
i = i + 1;
//Debug.Log(itemList[i]);
if (x == 1000) {
y = y + 10;
x = 0;
};
};
}
// Update is called once per frame
void Update() {
}
}