Object to appear in FULL in camera view no matter the size

Hi everyone. I’m very very new to Unity so please go easy on me but basically I have a problem that I do not know how to solve for the life of me. So I have to create a maze Generator(which I have done) but how can I make sure that the entire maze will be within camera view and with minimum white space? Any answers or code snippets would be very appreciated. Thank you so much.

This script will work with orthographic cameras. You can make the script work with perspective cameras but it will take a bit of tinkering. Added comments for further explanation. To work right, you need to make sure that the center of your maze and the camera are aligned.

using UnityEngine;

public class FillScreen : MonoBehaviour
    public float perspectiveCompensation = 0.95f;

    void Start()
        Camera cam = Camera.main;
        Bounds bounds = GetComponent<MeshRenderer>().bounds;
        Vector2 screenSize = new Vector2(Screen.width, Screen.height);

        //Get the position on screen.
        Vector2 screenPosition = cam.WorldToScreenPoint(bounds.center);
        //Get the position on screen from the position + the bounds of the object.
        Vector2 sizePosition = cam.WorldToScreenPoint(bounds.center + bounds.size);
        //By subtracting the screen position from the size position, we get the size of the object on screen.
        Vector2 objectSize = sizePosition - screenPosition;
        //Calculate how many times the object can be scaled up.
        Vector2 scaleFactor = screenSize / objectSize;
        //The maximum scale is the one form the longest side, with the lowest scale factor.
        float maximumScale = Mathf.Min(scaleFactor.x, scaleFactor.y);

        if (cam.orthographic)
            //Scale the orthographic size.
            cam.orthographicSize = cam.orthographicSize / maximumScale;
            //Set the scale of the object.
            transform.localScale = transform.localScale * maximumScale * perspectiveCompensation;