0
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.

1
  • 2
    for (int x = 0; y < mapWidth; x++) I think that middle value should be X, I've never seen a for loop written with a different variable, I can't see how that would work. Commented Jun 5, 2018 at 16:09

1 Answer 1

9

Your problem is here:

for (int x = 0; y < mapWidth; x++)

it should be:

for (int x = 0; x < mapWidth; x++)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.