Hi all,
I’m starting on setting up some random map generation, and have created two classes, one for holding map data and one for generating said data.
I’m currently getting a null reference exception from my generator. The actual noise generation script, whose functions I use, is not my own. It is the FastNoise library and is giving me no problems.
Here are my two scripts, it would be very nice if someone could look over them and let me know how stupid I’m being.
My map data holder:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MapData : MonoBehaviour
{
public int width;
public int height;
public float[,] landmassNoiseData;
public float[,] temperatureNoiseData;
void Start()
{
width = 100;
height = 100;
landmassNoiseData = new float[width,height];
temperatureNoiseData = new float[width,height];
}
}
My Generator:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MapGenerator : MonoBehaviour
{
public MapData mapdata;
FastNoiseLite noise;
IEnumerator Start()
{
yield return new WaitForSeconds(2f);
//coroutine for progress bar? eventually...
FastNoiseLite noise = new FastNoiseLite();
noise.SetNoiseType(FastNoiseLite.NoiseType.OpenSimplex2);
generateNoiseData(ref mapdata.landmassNoiseData);
generateNoiseData(ref mapdata.temperatureNoiseData);
//Debug.Log("landmass: " + mapdata.landmassNoiseData);
//Debug.Log("temp: " + mapdata.temperatureNoiseData);
}
//generates noise needed to randomly fill maps.
void generateNoiseData(ref float[,] data)
{
for (int x = 0; x < mapdata.height; x++)
{
for (int y = 0; y < mapdata.width; y++)
{
Debug.Log(data);
data[x,y] = noise.GetNoise(y, x);
}
}
}
}
The error pops up on line 32 of the generator. The specific error is “Object reference not set to an instance of an object” and refers to the data object I pass as a reference.
Thanks for any help!