Question by
SteamPunkDevil_ · Apr 20, 2016 at 12:00 AM ·
c#instantiateif statementwhile-loop
Error Instantiating Prefab in while look at end of row, Happens after a random amount of rows
I am working on a simple world generator using squares. But is its failing after a random number of rows, It does not instanate the last item at the end
Error:
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)
Code:
using UnityEngine;
using System.Collections;
using System.Linq;
//using System;
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() {
}
}
error.png
(307.5 kB)
Comment
Your answer
Follow this Question
Related Questions
Add changeable conditions in the editor, similar to unityEvent? 0 Answers
scroll rect UI buttons all do the same thing 0 Answers
I got Some Error about how to Transform Postiton Enemy where one point can spawn 2 enemies. 1 Answer
How does one continuously move an object forward without using velocity? 1 Answer