I need help with the error NullReferenceException

,

So I need help with my code, I keep getting a this error:

NullReferenceException: Object reference not set to an instance of an object
RoomGenerator.IsDirectionAvailable (System.Int32 direction) (at Assets/RoomGenerator.cs:88)
RoomGenerator.CalculateNextDirection () (at Assets/RoomGenerator.cs:81)
RoomGenerator.CalculatePosition (System.Int32 roomIndex) (at Assets/RoomGenerator.cs:47)
RoomGenerator.GenerateRooms () (at Assets/RoomGenerator.cs:28)
RoomGenerator.Start () (at Assets/RoomGenerator.cs:11)

And this is my script:

using UnityEngine;

public class RoomGenerator : MonoBehaviour
{
    public GameObject[] roomPrefabs;
    private GameObject[] generatedRooms;
    private bool[] roomOccupied;

    private void Start()
    {
        GenerateRooms();
    }

    private void GenerateRooms()
    {
        int numRoomsToGenerate = 9; // Generate 9 rooms

        generatedRooms = new GameObject[numRoomsToGenerate];
        roomOccupied = new bool[numRoomsToGenerate];

        // Create the first room with two openings at the starting position.
        generatedRooms[0] = Instantiate(roomPrefabs[0], Vector3.zero, Quaternion.identity);
        roomOccupied[0] = true;

        for (int i = 1; i < numRoomsToGenerate; i++)
        {
            // Calculate the position for the next room based on openings.
            Vector3 nextPosition = CalculatePosition(i);

            if (nextPosition == Vector3.zero)
            {
                // No valid position found, stop generating rooms.
                break;
            }

            // Determine the room prefab based on order.
            int roomPrefabIndex = i % roomPrefabs.Length;

            generatedRooms[i] = Instantiate(roomPrefabs[roomPrefabIndex], nextPosition, Quaternion.identity);
            roomOccupied[i] = true;
        }
    }

    private Vector3 CalculatePosition(int roomIndex)
    {
        Vector3 currentPosition = generatedRooms[roomIndex - 1].transform.position;
        int randomDirection = CalculateNextDirection();

        Vector3 nextPosition = currentPosition;
        float xOffset = 0.43f;
        float yOffset = 0.43f;

        switch (randomDirection)
        {
            case 0: // Move left
                nextPosition += new Vector3(-xOffset, 0, 0);
                break;
            case 1: // Move right
                nextPosition += new Vector3(xOffset, 0, 0);
                break;
            case 2: // Move down
                nextPosition += new Vector3(0, -yOffset, 0);
                break;
            case 3: // Move up
                nextPosition += new Vector3(0, yOffset, 0);
                break;
        }

        return nextPosition;
    }

    private int CalculateNextDirection()
    {
        int randomDirection;
        int maxAttempts = 4; // Maximum attempts to find an available direction.

        do
        {
            randomDirection = Random.Range(0, 4);
            maxAttempts--;
        } while (maxAttempts > 0 && !IsDirectionAvailable(randomDirection));

        return randomDirection;
    }

    private bool IsDirectionAvailable(int direction)
    {
        Vector3 currentPosition = generatedRooms[generatedRooms.Length - 1].transform.position;

        switch (direction)
        {
            case 0: // Check left
                currentPosition += new Vector3(-0.86f, 0, 0);
                break;
            case 1: // Check right
                currentPosition += new Vector3(0.86f, 0, 0);
                break;
            case 2: // Check down
                currentPosition += new Vector3(0, -0.86f, 0);
                break;
            case 3: // Check up
                currentPosition += new Vector3(0, 0.86f, 0);
                break;
        }

        // Check if the position is occupied by another room.
        Collider2D hitCollider = Physics2D.OverlapCircle(currentPosition, 0.01f);

        return (hitCollider == null);
    }
}

My script was made for a small 2d project in which case it would generate a map layout for a game and each room would be placed on a grid, the rooms were left open, right open, top open, bottom open, bottom and left open, bottom and right open, bottom and top open, top and left open, top and right open, and left and right open.

The object at generatedRooms[generatedRooms.Length - 1] is null. You don’t assign a value to that index before you attempt to read from it.

NullReferenceExceptions are always the same, understand the stack trace, what can be null, debug what it is, and then make sure it doesn’t occur by assigning a value, or checking for null.