Skip to main content
fixed typos, removed procedural generation tag since the question is far more about debugging null references - the fact that it happened in pro-gen type code has little bearing on the problem
Source Link
Pikalek
  • 13.4k
  • 5
  • 49
  • 54

Why am iI getting a "NullReferenceException: Object reference not set to an instance of an object" Error when calling List.Contains()?

So imI'm trying to make a randomized generation engine for a top down dungeon crawler, in the Binding of Isaac Stylestyle, however, the code is throwing out a multitude of errors when iI try it compare a value to a value in the list. theThe errors are on linelines 44,155,271 155,384 271, and 384 which are all ifif statements that are all formatted as such  :if(gameMaster.PositionsOfRooms.Contains(generatorPoint.transform.position))

if(gameMaster.PositionsOfRooms.Contains(generatorPoint.transform.position))

Code Snippet:

public class DoorGenerator : MonoBehaviour

public bool isdoor;
public bool facingUp;
public bool facingDown;
public bool facingRight;
public bool facingLeft;
public bool generated;
public bool startingRoom = false;
public int runningRoomCount;
public GameObject generatorPoint;
public GameMasterScript gameMaster;
public Transform teleportfromCords;
public Transform teleporttoCords;
public GameObject camera;
// Start is called before the first frame update
void Start()
{
    gameMaster = GameObject.Find("GameMaster").GetComponent<GameMasterScript>();
    teleportfromCords = transform.Find("TeleportPoint");
    camera = GameObject.Find("Main Camera");
}

// Update is called once per frame
void Update()
{
    if(generated == false && gameMaster.targetRooms > gameMaster.roomsGenerated) 
    {
        GenerateRoom();


    }
}

public void GenerateRoom()
{if(generated ==false){
generated = true;
{
    if(facingUp == true)
        {
        generatorPoint = transform.parent.Find("CreateRoomUp").gameObject;
        if(gameMaster.PositionsOfRooms.Contains(new Vector3(generatorPoint.transform.position.x, generatorPoint.transform.position.y, generatorPoint.transform.position.z)))
        {
            return;
        }
        else
        {

Why am i getting a "NullReferenceException: Object reference not set to an instance of an object" Error when calling List.Contains()?

So im trying to make a randomized generation engine for a top down dungeon crawler, in the Binding of Isaac Style, however, the code is throwing out a multitude of errors when i try it compare a value to a value in the list. the errors are on line 44,155,271,384, which are all if statements that are all formatted as such  :if(gameMaster.PositionsOfRooms.Contains(generatorPoint.transform.position))

Code Snippet:

public class DoorGenerator : MonoBehaviour

public bool isdoor;
public bool facingUp;
public bool facingDown;
public bool facingRight;
public bool facingLeft;
public bool generated;
public bool startingRoom = false;
public int runningRoomCount;
public GameObject generatorPoint;
public GameMasterScript gameMaster;
public Transform teleportfromCords;
public Transform teleporttoCords;
public GameObject camera;
// Start is called before the first frame update
void Start()
{
    gameMaster = GameObject.Find("GameMaster").GetComponent<GameMasterScript>();
    teleportfromCords = transform.Find("TeleportPoint");
    camera = GameObject.Find("Main Camera");
}

// Update is called once per frame
void Update()
{
    if(generated == false && gameMaster.targetRooms > gameMaster.roomsGenerated) 
    {
        GenerateRoom();


    }
}

public void GenerateRoom()
{if(generated ==false){
generated = true;
{
    if(facingUp == true)
        {
        generatorPoint = transform.parent.Find("CreateRoomUp").gameObject;
        if(gameMaster.PositionsOfRooms.Contains(new Vector3(generatorPoint.transform.position.x, generatorPoint.transform.position.y, generatorPoint.transform.position.z)))
        {
            return;
        }
        else
        {

Why am I getting a "NullReferenceException: Object reference not set to an instance of an object" Error when calling List.Contains()?

I'm trying to make a randomized generation engine for a top down dungeon crawler, in the Binding of Isaac style, however, the code is throwing out a multitude of errors when I try it compare a value to a value in the list. The errors are on lines 44, 155, 271, and 384 which are all if statements that are all formatted as such:

if(gameMaster.PositionsOfRooms.Contains(generatorPoint.transform.position))

Code Snippet:

public class DoorGenerator : MonoBehaviour

public bool isdoor;
public bool facingUp;
public bool facingDown;
public bool facingRight;
public bool facingLeft;
public bool generated;
public bool startingRoom = false;
public int runningRoomCount;
public GameObject generatorPoint;
public GameMasterScript gameMaster;
public Transform teleportfromCords;
public Transform teleporttoCords;
public GameObject camera;
// Start is called before the first frame update
void Start()
{
    gameMaster = GameObject.Find("GameMaster").GetComponent<GameMasterScript>();
    teleportfromCords = transform.Find("TeleportPoint");
    camera = GameObject.Find("Main Camera");
}

// Update is called once per frame
void Update()
{
    if(generated == false && gameMaster.targetRooms > gameMaster.roomsGenerated) 
    {
        GenerateRoom();


    }
}

public void GenerateRoom()
{if(generated ==false){
generated = true;
{
    if(facingUp == true)
        {
        generatorPoint = transform.parent.Find("CreateRoomUp").gameObject;
        if(gameMaster.PositionsOfRooms.Contains(new Vector3(generatorPoint.transform.position.x, generatorPoint.transform.position.y, generatorPoint.transform.position.z)))
        {
            return;
        }
        else
        {
Source Link

Why am i getting a "NullReferenceException: Object reference not set to an instance of an object" Error when calling List.Contains()?

So im trying to make a randomized generation engine for a top down dungeon crawler, in the Binding of Isaac Style, however, the code is throwing out a multitude of errors when i try it compare a value to a value in the list. the errors are on line 44,155,271,384, which are all if statements that are all formatted as such :if(gameMaster.PositionsOfRooms.Contains(generatorPoint.transform.position))

Code Snippet:

public class DoorGenerator : MonoBehaviour

public bool isdoor;
public bool facingUp;
public bool facingDown;
public bool facingRight;
public bool facingLeft;
public bool generated;
public bool startingRoom = false;
public int runningRoomCount;
public GameObject generatorPoint;
public GameMasterScript gameMaster;
public Transform teleportfromCords;
public Transform teleporttoCords;
public GameObject camera;
// Start is called before the first frame update
void Start()
{
    gameMaster = GameObject.Find("GameMaster").GetComponent<GameMasterScript>();
    teleportfromCords = transform.Find("TeleportPoint");
    camera = GameObject.Find("Main Camera");
}

// Update is called once per frame
void Update()
{
    if(generated == false && gameMaster.targetRooms > gameMaster.roomsGenerated) 
    {
        GenerateRoom();


    }
}

public void GenerateRoom()
{if(generated ==false){
generated = true;
{
    if(facingUp == true)
        {
        generatorPoint = transform.parent.Find("CreateRoomUp").gameObject;
        if(gameMaster.PositionsOfRooms.Contains(new Vector3(generatorPoint.transform.position.x, generatorPoint.transform.position.y, generatorPoint.transform.position.z)))
        {
            return;
        }
        else
        {