Using multiple yields in a Coroutine

Hi Guys,

Quick question about coroutines here; I’m setting up spawn system that utilizes a Coroutine and I’m running into some problems while trying to use several (yield return new) statements. Basically, the idea is to have a coroutine with two timers running inside. One is controlling the rate at which my while loop executes (and instantiates some game objects) and the other controls the length at which the coroutine runs for. At this point I’m not sure if I can reach the desired effect using just a Coroutine or if I should just write a custom function that gets called from Update.

Thanks for looking!

private IEnumerator Spawn(ObjectType type, float setLength, float spawnRate) {

		if (type == ObjectType.B) {

			while (true) {

				for (int i = 0; i < points.Count; i++) {
					
					Instantiate(ball, points_.transform.position, points*.transform.rotation);*_

* }*

* yield return new WaitForSeconds(spawnRate);*
* }*
* }*

* yield return new WaitForSeconds(setLength);*
* Debug.Log(“Reached the end”);*

* }*

What you are doing here will not do what I believe you are trying to do. Try this instead (untested):

private IEnumerator Spawn(ObjectType type, float setLength, float spawnRate) {
	
	if (type == ObjectType.B) {
		
		float timestamp = Time.time + setLength;

		while (Time.time < timestamp) {
			
			for (int i = 0; i < points.Count; i++) {
				
				Instantiate(ball, points_.transform.position, points*.transform.rotation);*_

* }*

* yield return new WaitForSeconds(spawnRate);*
* }*
* }*

* Debug.Log(“Reached the end”);*
* }*
Note that multiple yields in coroutines work, and there are other things you an do (like yield one coroutine on another), but what you are doing with your code here does not work, and multiple coroutines would not be the right way to solve this specific problem.