Alternative Solution, using timers and update()

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class ObjectPool : MonoBehaviour

{

[SerializeField] float waitTimer = 2f;

[SerializeField] float spawnTimer = 0f;

[SerializeField] GameObject enemy;

// Start is called before the first frame update

void Start()

{

   

}

// Update is called once per frame

void Update()

{

    spawnTimer += Time.deltaTime;

    if (waitTimer < spawnTimer)

    {

        SpawnEnemy();

        spawnTimer = spawnTimer - waitTimer;

    }

}

void SpawnEnemy()

{

    Instantiate<GameObject>(enemy, transform.position, Quaternion.identity);

}

}

1 Like

I like this approach. It was what I naturally thought to do (but didn’t know how), until I looked at the previous code snippet and saw the yield/coroutine approach.

Privacy & Terms