C# - Class returning error "NullReferenceException: Object reference not set to..."

I’m getting the following error: “NullReferenceException: Object reference not set to an instance of an object.” I get it for the line “roomBounds.max = Vector2.zero;” below. Am I setting something up incorrectly?

using UnityEngine;
using System.Collections;


public class RoomBounds {
	public Vector2 min;
	public Vector2 max;

	public RoomBounds (Vector2 minRef, Vector2 maxRef) {
		min = minRef;
		max = maxRef;
	}
}


public class RoomControl : MonoBehaviour {
	
	private RoomBounds roomBounds;
	
	
	void Start () {
		roomBounds.max = Vector2.zero;
	}
}

Yes. You need to add:

roomBounds = new RoomBounds(Vector2.zero, Vector2.zero);

Add that to your start method… but replace the “min” and “max” values with the Vectors you want. You haven’t instantiated an instance of RoomBounds which is why you are getting the error.

Oh duh! Thanks :slight_smile: