Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Using coroutine to activate pooled objects every x seconds

Discussion in 'Scripting' started by winterfive, Sep 24, 2018.

  1. winterfive

    winterfive

    Joined:
    May 21, 2018
    Posts:
    31
    Hi all,

    I am refactoring a project where I used InvokeRepeating to spawn my enemy prefabs("spawn" refers to setting active to true as I am pooling my objects). I am having difficulty replacing InvokeRepeating with a coroutine.

    Here's the code:
    Code (CSharp):
    1.  
    2. void Start()
    3. {
    4.     StartCoroutine(SpawnAttackDrone());
    5. }
    6.  
    7.  
    8. private IEnumerator SpawnAttackDrone()
    9. {
    10.     _activeAttackDrone = _poolManager.GetObjectFromPool(_attackDrones);
    11.  
    12.     while (_playerManager.IsAlive())
    13.     {
    14.         if (_activeAttackDrone)
    15.         {
    16.             Transform startPoint = GetRandomValueFromArray(spawnPoints);
    17.             _activeAttackDrone.transform.position = startPoint.position;
    18.             _activeAttackDrone.transform.rotation = startPoint.rotation;
    19.             _activeAttackDrone.gameObject.SetActive(true)
    20.         }
    21.         else
    22.         {
    23.             Debug.Log("There aren't any attackDrones available right now.");
    24.         }
    25.    }      
    26.        
    27.    yield return new WaitForSeconds(timeBetweenSpawns);
    28.    }
    Note: For now, I have the bool isAlive set as true while Time.time < 30f (in the PlayerManager class). Once it hits 30 seconds, isAlive becomes false. I've debugged this to check the values and it works.

    If I run this w/ the while loop, the editor crashes. If I remove the while loop, it runs once (spawns one prefab). I've looked at the docs, the unity videos, and googled for help and didn't find much. This project is for mobile VR so I am trying to be efficient (coroutines > invokeRepeating). Thoughts? Thanks!
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,366
    I think you want to move the
    yield return
    line inside the loop, moving the brace at line 25 down below line 27.

    You are currently saying "repeat the task until the player is dead, and THEN wait for a while." You want to be saying "repeat this task, and wait a while AFTER EACH time, until the player is dead." There doesn't seem to be any opportunity for the player to die during the task, so it is currently just spinning endlessly, either spamming the logs or calculating stuff like crazy.
     
    winterfive likes this.
  3. winterfive

    winterfive

    Joined:
    May 21, 2018
    Posts:
    31
    Gah! It's always a simple thing! That was indeed the issue. I also moved _activeAttackDrone = _poolManager.GetObjectFromPool(_attackDrones); into the loop as well. Now it spawns all the drones in the pool. Thanks!