Trying to destroy first object in a list gives "Index Out of Range" error

Hi! First time posting a question, very new to Unity!

I’m trying to make it so that after I’ve spawned a certain number of objects the first object spawned will be destroyed. I’ve gotten it to make a growing list of objects spawned and to count the number of objects spawned, but when the number of spawned objects passes the threshold I’ve set, instead of destroying ONE object in the list and removing it from the list, it destroys ALL objects spawned and removes ALL indexes from the list. Console gives me the error, “ArgumentOutOfRangeException: Index was out of range” at the line I’ve indicated in the Update function. I know that there’s something messed up about those two lines in the Update function but can’t for the life of me figure out what the problem is.

Any help is much appreciated!

  public class SpawnBalls : MonoBehaviour
    {
        public GameObject ball;
        private float startDelay = 1.0f;
        private float spawnInterval = 4.0f;
        private Vector3 spawnPos;
    
        public List<GameObject> spawnedObjects = new List<GameObject>();
        private int spawnCount = 0;
        private int ballSpawnThreshold = 3;
    
        void Start()
        {
            spawnPos = gameObject.transform.position;
            spawnInterval = Random.Range(3, 6);
            InvokeRepeating("SpawnBall", startDelay, spawnInterval);
        }
    
        void Update()
        {
            if (spawnCount > ballSpawnThreshold)
            {
                //code Console says is a problem:
                Destroy(spawnedObjects[0]);
                spawnedObjects.RemoveAt(0);
            }
        }
    
        void SpawnBall()
        {
            spawnedObjects.Add(Instantiate(ball, spawnPos, transform.rotation)
                );
            spawnCount++;
        }
    }

It’s probably because the List ran empty, and you tried calling an index that didn’t exist at that moment. I would suggest null checking before said code is to run:

if (spawnedObjects.Count > 0 && spawnCount > ballSpawnThreshold)
             {
                 Destroy(spawnedObjects[0]);
                 spawnedObjects.RemoveAt(0);
             }