How can I register each camera information script events onenable and ondisable ?

The main goal is to show the cameras current state/s.

The main script was before like this. The problem is that it was working fine but only once in the Start and I want it to update if there is any change in any of the cameras state enable/disable and display it in the List but I don’t want to make it loop over the foreach loops nonsotp in the Update so I’m stuck.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
using System.Linq;

public class CamerasInfo : MonoBehaviour
{
    public List<CinemachineFreeLook> FreeLook;
    public List<CinemachineVirtualCamera> Virtual;
    public List<Camera> AllCameras;
    public List<string> currentActiveCameras;

    // Start is called before the first frame update
    void Start()
    {
        Cameras();
    }

    // Update is called once per frame
    void Update()
    {
       
    }

    public void Cameras()
    {
        FreeLook = FindObjectsOfType<CinemachineFreeLook>().ToList();
        Virtual = FindObjectsOfType<CinemachineVirtualCamera>().ToList();
        for (int i = 0; i < Virtual.Count;)
        {
            if (!Virtual[i].name.StartsWith("CM"))
                Virtual.RemoveAt(i);
            else
                i++;
        }

        AllCameras = Camera.allCameras.ToList();
       
        foreach (CinemachineFreeLook freelook in FreeLook)
        {
            if (freelook.isActiveAndEnabled)
                currentActiveCameras.Add(freelook.Name);
        }

        foreach (CinemachineVirtualCamera vir in Virtual)
        {
            if (vir.isActiveAndEnabled)
                currentActiveCameras.Add(vir.Name);
        }

        for(int i = 0; i < AllCameras.Count; i++)
        {
            if (AllCameras[i].isActiveAndEnabled)
                currentActiveCameras.Add(AllCameras[i].name);
        }
    }
}

Then I thought to do it maybe in another way.
I created a small script not sure yet what and how to do the rest in the script that will be attached to each camera :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraInformation : MonoBehaviour
{
    public string cameraState;

    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
       
    }

    private void OnEnable()
    {
        cameraState = 
    }

    private void OnDisable()
    {
       
    }
}

And the first script I changed it to something like this :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
using System.Linq;

public class CamerasInfo : MonoBehaviour
{
    public List<CinemachineFreeLook> FreeLook;
    public List<CinemachineVirtualCamera> Virtual;
    public List<Camera> AllCameras;
    public List<string> currentActiveCameras;

    // Start is called before the first frame update
    void Start()
    {
        Cameras();
    }

    // Update is called once per frame
    void Update()
    {
       
    }

    public void Cameras()
    {
        FreeLook = FindObjectsOfType<CinemachineFreeLook>().ToList();
        Virtual = FindObjectsOfType<CinemachineVirtualCamera>().ToList();
        for (int i = 0; i < Virtual.Count;)
        {
            if (!Virtual[i].name.StartsWith("CM"))
                Virtual.RemoveAt(i);
            else
                i++;
        }

        AllCameras = Camera.allCameras.ToList();
       
        foreach (CinemachineFreeLook freelook in FreeLook)
        {
            freelook.gameObject.AddComponent<CameraInformation>();
        }

        foreach (CinemachineVirtualCamera vir in Virtual)
        {
            vir.gameObject.AddComponent<CameraInformation>();
        }

        for(int i = 0; i < AllCameras.Count; i++)
        {
            AllCameras[i].gameObject.AddComponent<CameraInformation>();
        }
    }
}

The main goal with this try is to display the current camera state/s in the main script CamerasInfo and also to display the current state of each camera in the small script CameraInfo.

but I’m not sure how to register and get the info from the OnEnable and OnDisable events.

My main goal is to know while the game is running what cameras are currently active and enabled and what are disabled. So if virtual camera 1 is active then at some point in the game virtual camera 1 is disable and main camera is enabled or both enabled or both disabled then update the strings at run time.

You would need to write a script that attaches to each camera that you can fire the OnEnable and OnDisable events from.

using Cinemachine;
using UnityEngine;
using UnityEngine.Events;

[System.Serializable]
public class CinemachineCamEnableChangedEvent : UnityEvent<CinemachineVirtualCameraBase, bool> { }

public class CinemachineCamHelper : MonoBehaviour
{
    public CinemachineCamEnableChangedEvent EnableChanged;

    protected CinemachineVirtualCameraBase virtualCam;

    public CinemachineVirtualCameraBase VirtualCam
    {
        get
        {
            if (virtualCam == null)
            {
                virtualCam = GetComponent<CinemachineVirtualCameraBase>();
            }

            return virtualCam;
        }
    }

    protected void OnEnable()
    {
        EnableChanged?.Invoke(VirtualCam, true);
    }

    protected void OnDisable()
    {
        EnableChanged?.Invoke(VirtualCam, false);
    }
}

The above class uses UnityEvents that are editor friendly so you can set it in the inspector if you so desire.

You can make use of a delegate

For example you have a class which needs to be informed about enable or disabled state of the camera ( or any game object )

class MyClass
{      
   public MyClass()
   {
       MyCameraClass  cam  = new MyCameraClass();
       cam.OnCameraEnable =  MyHandler  ;  
       cam.OnCameraDisable =  MyHandler  ;  
   }
 
   void MyHandler(object sender, EventArgs args)     
   {
      Debug.Log ( "The camera status has changed ") ;   
   }
}

//---------//---------//---------//---------//---------//---------//---------//---------

public delegate void MyDelegate(object sender, Eventargs args);
class MyCameraClass
{
    public event MyDelegate OnCameraEnable ;   
    public event MyDelegate OnCameraDisable ;   
    public MyCameraClass()
   {

                OnCameraEnable(this, EventArgs.Empty);
                OnCameraDisable(this, EventArgs.Empty);  
    }

}