Upon starting my game, I receive this error in the console:
NullReferenceException: Object reference not set to an instance of an object
Terrain_Generator.Start () (at Assets/Terrain_Generator.cs:28)
It says the prevRow object reference is not set even though it should be set in the code below:
using UnityEngine;
using System.Collections;
public class Terrain_Generator : MonoBehaviour
{
public GameObject Target_Obj;
public int width;
public int length;
public int height;
int prevPos;
int horizPos;
GameObject prevRow;
int prevRowPos;
int prevHigh;
int prevLow;
void Start ()
{
for (int j = 0; j < width + 1; j = j + 1)
{
for (int i = 0; i < length + 1; i = i + 1)
{
prevRowPos = j - 1;
horizPos = Random.Range (prevPos - height, prevPos + height + 1);
if(j >= 1)
{
prevRow = GameObject.Find ("Cube (" + i + "," + prevRowPos + ")");
if(horizPos > prevRow.transform.position.y)
{
horizPos = (int)prevRow.transform.position.y + 1;
}
if(horizPos < prevRow.transform.position.y)
{
horizPos = (int)prevRow.transform.position.y - 1;
}
}
Object.Instantiate (Target_Obj, new Vector3 (j, horizPos, i), Quaternion.identity);
prevPos = horizPos;
}
}
}
}