可能的初始化锯齿数组错误C#
我正在使用一个参差不齐的游戏对象数组来生成平铺地图,我不断得到错误: IndexOutOfRangeException:数组索引超出范围。 (包装器stelemref)对象:stelemref(对象,intptr,对象) Map.GenerateMap()(位于Assets/Scripts/Map.cs:148) Start()(位于Assets/Scripts/Map.cs:74) 我完全搞不懂为什么我会犯这个错误。下面是我用锯齿数组初始化编写的完整函数。据我所知,我正在正确初始化数组可能的初始化锯齿数组错误C#,c#,arrays,unity3d,C#,Arrays,Unity3d,我正在使用一个参差不齐的游戏对象数组来生成平铺地图,我不断得到错误: IndexOutOfRangeException:数组索引超出范围。 (包装器stelemref)对象:stelemref(对象,intptr,对象) Map.GenerateMap()(位于Assets/Scripts/Map.cs:148) Start()(位于Assets/Scripts/Map.cs:74) 我完全搞不懂为什么我会犯这个错误。下面是我用锯齿数组初始化编写的完整函数。据我所知,我正在正确初始化数组 voi
void GenerateMap(){
mapWidth = Random.Range (5, 25);
mapHeight = Random.Range (5, 25);
//Initialise the Jagged Array
genMapArray = new GameObject [mapHeight][];
for (int i = 0; i < mapHeight; i++)
genMapArray [i] = new GameObject [mapWidth];
//Generate some mountains
int mountX = Random.Range(1, mapWidth - 1); // Random within
int mountY = Random.Range(1, mapHeight - 1); // the map border
for (int y = 0; y < mapHeight+1; y++) {
for (int x = 0; x < mapWidth+1; x++) {
if((y == 0 && x <= mapWidth) || (y <= mapHeight & x == 0) ||
(y== mapHeight && x <= mapWidth) || (y <= mapHeight && x == mapWidth)){
genMapArray[y][x] = Tiles[0];
}else{
if(y == mountY && x == mountX){
genMapArray[y][x] = Tiles[3];
}else{
genMapArray[y][x] = Tiles[1];
}
}
}
}
//Draw Tiles on the Screen
for (int y = 0; y < mapHeight+1; y++) {
for (int x = 0; x < mapWidth+1; x++) {
if (useSquareTiles == true && useHexagonTiles == false) {
GameObject.Instantiate (genMapArray[y][x], new Vector3 (x, mapHeight - y, 0),
Quaternion.Euler (-90, 0, 0));
}
if (useHexagonTiles == true && useSquareTiles == false) {
float hexWidth = 0.75f / Mathf.Cos (Mathf.Deg2Rad * 30.0f);
GameObject.Instantiate (genMapArray[y][x], new Vector3 (x * hexWidth, y + (0.5f * Mathf.Abs (x) % 1), 0),
Quaternion.Euler (-90, 0, 0));
}
}
}
}
void GenerateMap(){
地图宽度=随机范围(5,25);
地图高度=随机范围(5,25);
//初始化锯齿状数组
genMapArray=新游戏对象[mapHeight][];
对于(int i=0;i 如果((y==0&&x)你能指出你从哪一行得到这个吗?for(int y=0;y
-我看不太对…为什么要使用+1
?你真的想要mapHeight
元素还是mapHeight+1
元素?我也尝试过+1是一个错误xD我发现了我的问题xD+1,并更改了边界的if语句xD我发现错误确实是+1,但我也不得不这么做更改边框的某些if语句。