Noise Map not generating

So I am following this tutorial by Sebastian Lague on procedural terrain generation. Here is the vid in question:

Near the end of the vid, he presses a generate button and the noise map he needed to make was formed. But when I try it, I get this error:

IndexOutOfRangeException: Index was outside the bounds of the array.
noise.GenerateNoiseMap (System.Int32 mapWidth, System.Int32 mapHeight, System.Single scale) (at Assets/Scripts/map/noise.cs:24)
mapmaker.generateMap () (at Assets/Scripts/map/mapmaker.cs:13)
mapGeneratorEditor.OnInspectorGUI () (at Assets/Scripts/map/editor/mapGeneratorEditor.cs:19)
UnityEditor.UIElements.InspectorElement+<>c__DisplayClass59_0.b__0 () (at <1ada6c7052bb42378c5ec1bd01fc4723>:0)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)

I believe the problem lies in this particular piece of code, I am pretty new to unity, so could someone help me please?

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 = Mathf.Clamp(scale, float.Epsilon, float.MinValue);
}
for(int y = 0; y <= mapHeight; y++)
{
for (int x = 0; x <= mapHeight; x++)
{
float sampleX = x;
float sampleY = y;
float perlinValue = Mathf.PerlinNoise(sampleX, sampleY);
noiseMap[x,y] = perlinValue;
}
}
return noiseMap;
}
}

In both your for loops replace the <= with just <. Arrays index from 0, you have to account for that.

It’s only part of the problem though.

A little help can go a long way, the video likely won’t explain (haven’t watched it) why exactly it needs to be that way so providing a little help alongside an explanation is a way better way to learn than to just say “Go watch the video again and get every character correct”, while that’s true it’s incredibly unhelpful, it’ll fix their issue, but they won’t understand why it fixed it.

And my bad on the correction, we all make mistakes. I’ve edited.

If anything, using a YouTube tutorial in the first place is a sign you don’t give a rat’s ass about why something works, only that it does. I’d say your explanation isn’t sufficient for why it’s wrong in the first place. We know arrays are 0-indexed. We literally started with x = 0 and y = 0. Sure, let’s play your little game, but provide explanations for all pieces involved.

First problem in original post: you made a mistake in the variable used for the condition in the for-loop using x. Iterating with x means you would want to go up to the mapWidth variable, since you’re starting from one end (probably representing the left side) and going over to the other end, a total of mapWidth cells.

Second problem: When iterating over the indices of an array or one dimension of a 2D array, valid indices of something of length N go from 0, 1, 2, …, N-1. With the way the bounds are set up in the shown code, you would be going from 0, 1, 2, …, N-1, N. That’s one too many. In general, you’d expect to have for-loops go over every element of an array (or every row or column in a 2D array) by using < instead of <= so you succeed on N-1 and fail (and end the loop) on N.

Third problem: the part of your code that adjusts scale when it’s less than or equal to zero is probably wrong here. You’re probably trying to make sure it’s within some bounds. I assume this is trying to make sure it’s a positive number. In this case, you don’t need an if-condition, you can just set scale = Mathf.Clamp(scale, yourMinValue, yourMaxValue) - clamp won’t do anything to the value if it’s in your bounds. I also believe your use of float.MinValue should be float.MaxValue, in order to make the clamp cover all positive values.

Good practice: coding convention is really good for making code “read well” to other developers. One thing you ought to do is capitalize the class name “noise” so it follows the established standard. There’s always going to be some leeway on some things, but capitalizing class names is pretty much a universal thing for C# code.

1 Like