Instantiate object after delay

Here is my code but after 2 seconds it only spawns 1 object and stops.

public class SpawnManager : MonoBehaviour
{
    
    public GameObject prefab;
    
    void Start()
    {
         
    }

    
    void Update()
    {
      StartCoroutine(InstantiateObjects(2f));
    }

    IEnumerator InstantiateObjects(float ct)
    {
       yield return new WaitForSeconds(ct);
        {
          Instantiate(prefab, new Vector3(2.0f, 0, 4), Quaternion.identity);
        }
    }
    
}

You should not use a coroutine like that. Instead of update, place it in start, and put contents of the coroutine (waitforseconds and instantiate) in while(true). Also you dont need those brackets under waitforseconds.

void Start ()
{
float inTime = 2f;
InvokeRepeating (nameof(Spawn), inTime);
}

void Spawn ()
{
    Instantiate(prefab, position, Quaternion.identity);
}

If you put a coroutine in the Update loop then it’s going to be called at each frame. And don’t use brackets after WaitForSeconds.

The way I would do it is this:

    float timer = 0;
    bool hasSpawned = false;
    
    void Update()
    {
         if (!hasSpawned)
         {
              timer++;
         }
         if (timer >= 100 && !hasSpawned)
         {
              //Instantiate the object
              hasSpawned = true;
         }
    }

I haven’t tested this yet, but I hope it helps!
(one more thing, ask chatGPT whatever you want to know like “How would I Instantiate object after delay in unity” and then once it has responded, if it hasn’t yet written some code, ask it “write some code” here’s a link: https://chat.openai.com/chat it’s quite cool!)