HDRP Depth of field change settings in script,Updating HDRP Depth of field in scripts

I have created a new Scene Settings and added a Depth of Field override to the volume that was created from that. In it, I set the focus mode to use physical camera, then changed the focus distance. What I want is to be able to dynamically change the focus distance from a script.

So far I have the following code which compiles, but does not change anything. Any help would be appreciated.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;

public class DOFController : MonoBehaviour
{
public GameObject DOFFocus;
private float dist;

public PostProcessVolume volume;
DepthOfField depthOfField;

// Start is called before the first frame update
void Start()
{
    volume.profile.TryGetSettings(out depthOfField);
}

// Update is called once per frame
void Update()
{
    dist = Vector3.Distance(transform.position, DOFFocus.transform.position);
    depthOfField.focusDistance.value = dist;
    volume.profile.GetSetting<DepthOfField>().focusDistance.value = dist;
}

} ,In Scene Settings, I added an override to the volume script to create a depth of field effect. In it I am using a physical camera where I set the focus distance and other settings. I want to be able to dynamically change the value for the Focus Distance using a script.

I’ve tried the following code, but with no results. Any help would be appretiated

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;

public class DOFController : MonoBehaviour
{
public GameObject DOFFocus;
private float dist;

public PostProcessVolume volume;
DepthOfField depthOfField;

// Start is called before the first frame update
void Start()
{
    volume.profile.TryGetSettings(out depthOfField);
}

// Update is called once per frame
void Update()
{
    dist = Vector3.Distance(transform.position, DOFFocus.transform.position);
    depthOfField.focusDistance.value = dist;
    volume.profile.GetSetting<DepthOfField>().focusDistance.value = dist;
}

}

` using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.HighDefinition;
public class setDefOffil : MonoBehaviour
{
private VolumeProfile profile;
private DepthOfField dof;

    void Start() {
        profile = GetComponent<Volume>().profile;
        profile.TryGet<DepthOfField>(out dof);
    }
 
    void Update() {
        RaycastHit hit;
        if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit)) {
            float dist = Vector3.Distance(Camera.main.transform.position, hit.point);
            dof.focusDistance.value = Mathf.Lerp(dof.focusDistance.value, dist, 0.25f);
        }
    }
}`

`

@ mtudisco742