About 'Revise Coroutines & Much More'!

In this video (objectives)…

  1. Show how to search our GitHub pages
  2. Explain spawning architecture
  3. Setup a spawning coroutine.

After watching (learning outcomes)…

Create a simple co-routine from scratch on your own. Remember "Blooms Taxonomy"

(Unique Video Reference: 26_RR_CU2)

We would love to know…

  • What you found good about this lecture?
  • What we could do better?

Remember that you can reply to this topic, or create a new topic. The easiest way to create a new topic is to follow the link in Resources. That way the topic will…

  • Be in the correct forum (for the course).
  • Be in the right sub-forum (for the section)
  • Have the correct lecture tag.

Enjoy your stay in our thriving community!

Umm, Rick already created this script in a previous video :thinking:

I’m afraid I re-created the previous video, sorry about the disruption. Please re-watch.

I went about the challenge a little differently and used recursion instead of an infinite while loop, so I thought I’d share my solution here:

IEnumerator SpawnEnemy () {
    Instantiate(enemyPrefab, startNode.transform.position, Quaternion.identity, transform);
    yield return new WaitForSeconds(SpawnDelay);
​    StartCoroutine(SpawnEnemy());
​}
1 Like

Interesting approach, though given this is a forever type loop, I am wondering how long this can run for?
Will it eventually crash?

It is my belief in other languages, that functions are stored on the stack and that using recursion too many times eats into this stack until it overruns. I am wondering if this is the case with C#/Unity?

Good call, I probably should have tested that.
It would indeed be an issue if it were left running for long enough. I didn’t really think about it at the time, but after doing some testing it looks like it chews through ~12Mb per minute on my machine.

In the grand scheme of things, the while loop is definitely a better option if you leave it running continuously but I went on to adapt this code to spawn waves, so once the recursion was given a base case the memory usage is no longer a major problem.

1 Like

I also went about the challenge a bit differently, setting the next spawn time after each spawn:

using UnityEngine;

public class EnemySpawner : MonoBehaviour
{
    [SerializeField] GameObject _enemyPrefab;
    [SerializeField] float _spawnInterval = 3f;

    private float _nextSpawnTime = 0f;

    void Update()
    {
        if (Time.time >= _nextSpawnTime)
        {
            SpawnEnemy();
            _nextSpawnTime = Time.time + _spawnInterval;
        }
    }

    private void SpawnEnemy()
    {
        var enemy = Instantiate(_enemyPrefab, transform.position, transform.rotation);
        enemy.transform.parent = transform;
    }
}

I’m not sure if a recent change to Unity has caused the issue or it’s something on my end, but I can’t get Instantiate to work when it’s an object trying to instantiate its own prefab. Suggestions for a workaround?

Edit: I watched the video again. I was attempting to place the spawner script on the wrong object. Works fine now. Nothing to see here. :blush:

Privacy & Terms