Script not working after restarting, giving null reference while it should work

Hey there, i ran into a problem.
I created a script which generates simple quads, which i eventually want to be a GUI minimap.
It worked perfectly! Until i restarted Unity, now its giving me an error once i call the Generate() function.

Its giving me a null reference error, which shouldn’t happen. In the start function i already set the LevelGenerator.
Once i set the LevelGenerator again in the Generate() function, than it “creates” quads but with weird sizes and which are not visible. Anyone encountered this problem before? Or knows how to fix this?

This is the script:

using UnityEngine;
using System.Collections;

public class QuadGenerate : MonoBehaviour
{
	public float width;
	public float height;
	
	public float gridHeight;
	public float gridWidth;
	
	public float iconWidth;
	public float iconHeight;
	
	public Material tileSet;
	public Texture2D texture;
	
	LevelGenerator level;
	private Transform myTransform;

	public void Awake()
	{
		myTransform = transform;
	}
	
	public void Start()
	{
		level = LevelGenerator.instance;

		gridWidth = level.width;
		gridHeight = level.height;
		
		iconWidth = width/gridWidth;
		iconHeight = height/gridHeight;
	}
	
	//DOESNT INSTANTIATE THE QUADS, AND CANT SEEM TO FIND THE LEVELGENERATOR
	public void Generate()
	{
		for(int x = 0; x < level.width; x++)		//ERROR
		{
			for(int y = 0; y < level.height; y++)
			{
				if(level.grid[x,y].type > 0)
				{
					GameObject tile = new GameObject();
					
					MeshFilter mf = tile.AddComponent<MeshFilter>();
					MeshRenderer mr = tile.AddComponent<MeshRenderer>();
					
					tile.name = string.Format("Icon: " + x + " | " + y + " | type: " + level.grid[x,y].type);
					mr.material = tileSet;
					Mesh mesh = GenerateMesh(x,y,level.grid[x,y].type);
					mf.mesh = mesh;
					
					mr.renderer.sharedMaterial.mainTexture = texture;
	
					tile.transform.position = new Vector3(x*iconWidth*2,y*iconHeight*2,0);
					
					tile.transform.parent = myTransform;
				}
			}
		}
	}
	
	
	public Mesh GenerateMesh(int x, int y, int type)
	{
		Mesh m = new Mesh();
		
		m.vertices = new Vector3[] {
			new Vector3(-iconWidth, -iconHeight, 0.01f),
			new Vector3(iconWidth, -iconHeight, 0.1f),
			new Vector3(iconWidth, iconHeight, 0.01f),
			new Vector3(-iconWidth, iconHeight, 0.01f) };
		
		m.uv = new Vector2[] {
			new Vector2(0,0),
			new Vector2(0,1),
			new Vector2(1,1),
			new Vector2(1,0) };
		
		m.triangles = new int[] { 0, 1, 2, 0, 2, 3 };
		m.RecalculateNormals();
		
		return m;
	}
}

Also, after i restarted Unity and the script is not working. If i reset the component, the script is fully functional again.

Cheers, Darryl

Just a guess but is in line 28 the instance in LevelGenerator simply null?

level = LevelGenerator.instance;

This would mean that the member variable level is null as well and it will crash at line 40. Have a look at the class LevelGenerator or show it to us so we can help :slight_smile:

okay, so at work I started to try to come up with a solution.
I basically took the script, deleted the Start() function, and tried it again just now.
I don’t know why, but it works. Even after restarting Unity. Here you can check the script:

using UnityEngine;
using System.Collections;

public class MiniMap : MonoBehaviour
{
	public float width;
	public float height;
	
	public float gridHeight;
	public float gridWidth;
	
	public float iconWidth;
	public float iconHeight;
	
	public Material tileSet;
	
	private LevelGenerator level;
	private Transform myTransform;
	
	public void Generate()
	{
		level = LevelGenerator.instance;
		myTransform = transform;
		
		gridWidth = level.width;
		gridHeight = level.height;
		
		iconWidth = width/gridWidth;
		iconHeight = height/gridHeight;
		
		for(int x = 0; x < gridWidth; x++)
		{
			for(int y = 0; y < gridHeight; y++)
			{
				if(level.grid[x,y].type > 0)
				{
					GameObject tile = new GameObject();
					
					MeshFilter mf = tile.AddComponent<MeshFilter>();
					MeshRenderer mr = tile.AddComponent<MeshRenderer>();
					
					tile.name = string.Format("Icon: " + x + " | " + y);
					mr.material = tileSet;
					Mesh mesh = GenerateMesh(x,y);
					mf.mesh = mesh;
					
					tile.transform.position = new Vector3(x*iconWidth*2,y*iconHeight*2,0);
					
					tile.transform.parent = myTransform;
				}
			}
		}
	}
	
	
	public Mesh GenerateMesh(int x, int y)
	{
		Mesh m = new Mesh();
		
		m.vertices = new Vector3[] {
			new Vector3(-iconWidth, -iconHeight, 2),
			new Vector3(iconWidth, -iconHeight, 2),
			new Vector3(iconWidth, iconHeight, 2),
			new Vector3(-iconWidth, iconHeight, 2) };
		
		m.uv = new Vector2[] {
			new Vector2(0,0),
			new Vector2(0,1),
			new Vector2(1,1),
			new Vector2(1,0) };
		
		m.triangles = new int[] { 0, 1, 2, 0, 2, 3 };
		m.RecalculateNormals();
		
		return m;
	}
}

Well anyway luckily it worked.

Cheers, Darryl.