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:


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) 


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)))

2 Answers 2


A NullReferenceException means you tried to "reach into" an object (ie. with a .dot or an [index]) while that object reference is set to null, meaning that there's no object there to reach into. It's like if I asked you what color your car is, and you don't own a car: there's no valid answer to that question because the premise is wrong.

If you're saying gameMaster.PositionsOfRooms.Contains(new Vector3(generatorPoint.transform.position.x, generatorPoint.transform.position.y, generatorPoint.transform.position.z)), then one of gameMaster, gameMaster.PostionsOfRooms, or generatorPoint is null here. You need to figure out which one it is and correct the code, either by fixing it so that it won't be null here, or by checking for null first and not calling this if the objects you're relying on aren't available.

  • \$\begingroup\$ Would therre be an easy Debug.Log() method to try and find which variable is null? \$\endgroup\$ Jan 13 at 20:23
  • \$\begingroup\$ @Thirstierpoet17 Debug.Log($"gameMaster: {gameMaster?.Name ?? "null"}"); if (gameMaster != null) {Debug.Log($"rooms: {gameMaster.PositionsOfRooms?.Count ?? "null"}");} \$\endgroup\$ Jan 13 at 20:32
  • 1
    \$\begingroup\$ That may look a little bit arcane if you're new to C#, but it will do the trick, and once you understand how it works, you'll be a better programmer for it. :) \$\endgroup\$ Jan 13 at 20:33
  • \$\begingroup\$ Or simply set a break point and inspect the value of your object. But this here 'transform.parent.Find("CreateRoomUp").gameObject' looks weird as well. And since this assign to generator point and you try to reach a sub element from it, chances are this is your mistake \$\endgroup\$
    – Zibelas
    Jan 13 at 20:47

In your if statement you are searching for a newly created position instance - very likely the newly created instance will never be in the list.

As of my understanding you want to check if the newly generated position value is inside the list of possible position values right?

If so you could do something like this:

    // this is your gameMaster-List
    List<Transform> positions = new();

    // this is your newly created position
    Vector3 positionToCheck = new(0, 0, 0);

    // this is the newly created position but in string format
    string positionToCheckString = positionToCheck.ToString();

    // start to loop through your avaible position
    foreach (Transform avaiblePosition in positions)
        // optional null check
        if (avaiblePosition == null) continue;

        // check if string of an position instance is equal to your newly created position
        if (avaiblePosition.ToString() == positionToCheckString)
            // the value is inside the list
            // the value is not in the list

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .