简体   繁体   English

Unity:索引超出数组范围

[英]Unity: Index was outside the bounds of the Array

im developing with Unity for about a year now and very often I got this Error: IndexOutOfRangeException: Index was outside the bounds of the array.我用 Unity 开发了大约一年,我经常收到这个错误:IndexOutOfRangeException:索引超出了数组的范围。 I know that that error means that the given index is bigger then the array itself.我知道那个错误意味着给定的索引比数组本身大。 But I dont think that the Array is bigger than the used value.但我不认为数组大于使用值。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ProceduralGeneration : MonoBehaviour
{
public GameObject[,] tiles = new GameObject[3000, 3000];
public GameObject[] prefabs;
int sizeyet = 0;
GameObject obj;
public int renderdistance;

int[,] map;

// Start is called before the first frame update
void Start()
{
    map = CreateMap(map);
    for (int i = 0; i < map.Length; i++)
    {
        for (int j = 0; j < map.Length; j++)
        {
            
            
            obj = Instantiate(prefabs[map[i,j]], new Vector2(i, j), Quaternion.identity);
            tiles[i, j] = obj;

            
        } 
    }
}

public int[,] CreateMap(int[,] map)
{
    map = new int[4000, 4000];
    for(int i = 0; i < 3000 - 1; i++)
    {
        for(int j = 0; i < 3000 - 1; j++)
        {
            map[i, j] = Mathf.RoundToInt(Mathf.PerlinNoise(i, j) * 10);
        }
    }
    return map;
}

// Update is called once per frame
void Update()
{
    
}

} }

I very often canceled project because I couldn't fix this error.我经常取消项目,因为我无法修复此错误。

If my English is not so good, excuse me.如果我的英语不太好,请原谅。 Im a german student.我是德国学生。

There are a few problems here.这里有一些问题。 Firstly, there's a typo in CreateMap :首先, CreateMap中有一个错字:

for(int j = 0; i < 3000 - 1; j++)

Note how you're using i in the condition instead of j - so that loop will never terminate, assuming that i is less than 2999 to start with.请注意您是如何在条件中使用i而不是j - 这样循环将永远不会终止,假设i小于 2999 开始。 But even changing that won't fix the bigger problem.但即使改变它也无法解决更大的问题。 Let's look at the rest of the code.我们看一下代码的rest。

Here's how you initialize map :以下是初始化map的方法:

map = new int[4000, 4000];

And here's how you initialize tiles :下面是初始化tiles的方法:

public GameObject[,] tiles = new GameObject[3000, 3000];

So map.Length is 16000000, because that's the total size of the array .所以map.Length是 16000000,因为这是数组的总大小

Now let's look at your loop:现在让我们看看你的循环:

for (int i = 0; i < map.Length; i++)
{
    for (int j = 0; j < map.Length; j++)
    {
        obj = /* irrelevant */
        tiles[i, j] = obj;
    } 
}

So your code is a bit like this:所以你的代码有点像这样:

GameObject[,] tiles = new GameObject[3000, 3000];
for (int i = 0; i < 16000000; i++)
{
    for (int j = 0; j < 16000000; j++)
    {
        tiles[i, j] = something;
    }
}

Can you see how that's clearly going to go way out of bounds?你能看出 go明显越界了吗?

Even if the code behaved as you expected it to, tiles is 3000x3000 and map is 4000x4000, so that would be a problem.即使代码的行为符合您的预期, tiles为 3000x3000 而map为 4000x4000,因此这将是一个问题。

I suggest you create some constants, and use those everywhere:我建议你创建一些常量,并在任何地方使用它们:

private const int MapWidth = 4000;
private const int MapHeight = 4000;

public GameObject[,] tiles = new GameObject[MapWidth, MapHeight];
...

// Then later...
for (int i = 0; i < MapWidth; i++)
{
    for (int j = 0; j < MapHeight; j++)
    {
        tiles[i, j] = ...;
    }
}

// And in CreateMap:
map = new int[MapWidth, MapHeight];
for (int i = 0; i < MapWidth; i++)
{
    for (int j = 0; j < MapHeight; j++)
    {
        map[i, j] = Mathf.RoundToInt(Mathf.PerlinNoise(i, j) * 10);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM