IEnumerator not working when i restart the game

using UnityEngine;
using System.Collections;
using AI_Enemy;

public class RandomSpawner : MonoBehaviour
{
    public GameObject cop;
    public GameObject robber;
    public GameObject money;
    public GameObject gem;
    public GameObject wayPoints;
    [SerializeField] private int copsToSpawn = 5;
    [SerializeField] private float moneySpawnInterval = 5f;
    [SerializeField] private float gemSpawnInterval = 15f;
    private GameObject[] _floor;

    private void Start()
    {
        StartCoroutine(DelayedStart());
    }

    public IEnumerator DelayedStart()
    {
        yield return new WaitForSeconds(0.1f);

        _floor = GameObject.FindGameObjectsWithTag("Floor");
        if (_floor.Length == 0)
        {
            Debug.LogError("No floor objects found. Initialization failed.");
            yield break;
        }

        // Once _floor is initialized, start spawning objects
        SpawnPlayer();
        SpawnCops();
        StartCoroutine(SpawnMoney());
        StartCoroutine(SpawnGems());
    }

    private Vector3 FindFloorToSpawn()
    {
        var randomFloor = _floor[Random.Range(0, _floor.Length)];
        var randomPositionX = randomFloor.transform.localScale.x / 2;
        var randomPositionZ = randomFloor.transform.localScale.z / 2;

        // Generate a random position on the floor object
        return randomFloor.transform.position + new Vector3(Random.Range(-randomPositionX, randomPositionX),
            1, Random.Range(-randomPositionZ, randomPositionZ));
    }

    private void SpawnPlayer()
    {
        var playerPosition = new Vector3(0, 1, 0);
        var playerInstance = Instantiate(robber, playerPosition, Quaternion.identity);

        var cameraFollow = Camera.main!.GetComponent<CameraFollow>();
        if (cameraFollow != null)
        {
            cameraFollow.target = playerInstance.transform;
        }
    }

    private void SpawnCops()
    {
        for (var i = 0; i < copsToSpawn; i++)
        {
            var wayPointPosition = FindFloorToSpawn();

            var newWayPoints = Instantiate(wayPoints, wayPointPosition, Quaternion.identity);
            var newCop = Instantiate(cop, wayPointPosition, Quaternion.identity);

            var aiEnemy = newCop.GetComponent<AIEnemy>();
            if (aiEnemy != null)
            {
                aiEnemy.AssignWaypoints(newWayPoints.transform);
            }
        }
    }

    private IEnumerator SpawnMoney()
    {
        while (true)
        {
            yield return new WaitForSeconds(moneySpawnInterval);
            var randomPosition = FindFloorToSpawn();
            Instantiate(money, randomPosition, Quaternion.identity);
        }
    }

    private IEnumerator SpawnGems()
    {
        while (true)
        {
            yield return new WaitForSeconds(gemSpawnInterval);
            var randomPosition = FindFloorToSpawn();
            Instantiate(gem, randomPosition, Quaternion.Euler(-90, 0, 0));
        }
    }
}

Sounds like you wrote a bug… and that means… time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.

If you have an actual question, try this format:

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, log output, variable values, and especially any errors you see
  • links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)

The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?

If you post code, only post the relevant code and always use the format button above. Do not post photographs of code.

so what doesn’t work?

also never rely on specific time to ensure your game objects are ready to use. sometimes that 0.f seconds may be enough sometime not and the code will fail