Reputation: 358
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class noise
{
public static float[,] GenerateNoiseMap(int mapWidth, int mapHeight, float scale)
{
float[,] noiseMap = new float[mapWidth, mapHeight];
if (scale <= 0)
{
scale = 0.0001f;
}
for(int y = 0; y<mapHeight; y++)
{
for (int x = 0; y < mapWidth; x++)
{
float sampleX = x/(mapWidth * scale);
float sampleY = y/(mapHeight * scale);
float perlinValue = Mathf.PerlinNoise(sampleX, sampleY);
noiseMap[x, y] = perlinValue;
Debug.Log(noiseMap.GetLength(0));
Debug.Log(noiseMap.GetLength(1));
}
}
return noiseMap;
}
}
Above is the code that is giving me the error:
IndexOutOfRangeException: Array index is out of range.
noise.GenerateNoiseMap (Int32 mapWidth, Int32 mapHeight, Single scale) (at
Assets/Scripts/MapGeneration/noise.cs:24)
I'm not entirely familiar with how arrays work in C# (or at all) but my teachers says that the most common issue with arrays that yield an IndexOutOfRange error is that I'm starting at 1 instead of index 0. I've tried to fix this but that doesn't seem to be the problem in this section of code.
I am attempting to generate a Perlin noise map for my custom game.
What is throwing the error?
Thanks in advance.
Upvotes: 0
Views: 171
Reputation: 3065
Your problem is here:
for (int x = 0; y < mapWidth; x++)
it should be:
for (int x = 0; x < mapWidth; x++)
Upvotes: 9