Optimization problems

Hello, I’ve been trying to make a script to disable objects that are not in view of the main camera, this is what I have so far:

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

public class Map : MonoBehaviour {
    public GameObject[] gameObjects;
    public float x;
    public float y;

    private void Update()
    {
        UpdateObjects(gameObjects);
    }

    void UpdateObjects(GameObject[] objects)
    {
        foreach(GameObject g in objects)
        {
            x = Camera.main.WorldToScreenPoint(g.transform.position).x;
            y = Camera.main.WorldToScreenPoint(g.transform.position).y;
            if (x < 0 || x > Screen.width)
            {
                g.SetActive(false);
            }
            else if (y < 0 || y > Screen.height)
            {
                g.SetActive(false);
            }
            else
            {
                g.SetActive(true);
            }
        }
       
    }
}

This partially works, however, objects behind (behind being a cast of FOV of the camera backwards) the camera still are enabled, thanks for any responses!

example images: Imgur: The magic of the Internet

This should help - Unity - Scripting API: MonoBehaviour.OnBecameInvisible()

Although it would be worth looking into Unity’s occulsion culling

public GameObject[] gameObjects;
   
public Camera cam;
    private void Update()
    {
       if(Time.frameCount % 5 == 0)
       {
        for(int i=0;i<gameObjects.Length;i++)
        {
Vector3 pos = cam.WorldToScreenPoint(gameObjects[i].transform.position);
            if (pos.x < 0 || pos.x > Screen.width)
            {
                gameObjects[i].SetActive(false);
            }
            else if (pos.y < 0 || pos.y > Screen.height)
            {
                gameObjects[i].SetActive(false);
            }
            else
            {
                gameObjects[i].SetActive(true);
            }
        }
    }
  
    }