Problem Using Coroutine

Hi! I’m trying to create script to randomly spawns object using coroutine from the video tutorial i found on youtube:

I follows the steps but using different variable names, but i got some error that doesn’t exist in the video (shown in the picture below) what should i do to fix this? and below i also paste my code

using UnityEngine;
using System.Collections;

public class Spawner : MonoBehaviour, TimedInputHandler
{
public GameObject[ ] sampah;
public Vector3 spawnRange;

private int randSampah;
private int trashCount=0;

// Use this for initialization
void Start()
{

}

// Update is called once per frame
void Update()
{

}

public void HandleTimedInput()
{
IEnumerator nyampah()
{
yield return new WaitForSeconds(1);
while(trashCount<11)
{

trashCount++;
}
}
}
}


I can’t help you with your problem, but I advice you to use CodeTags, so code is more readable:

2 Likes

i’m guessing that HandleTimedInput is the interface method that is being implemented? if so, i would guess it should be written like this.

public IEnumerator HandleTimedInput()
{
      yield return new WaitForSeconds(1);
      while (trashCount < 11)
             trashCount++;
}

i’m not sure the purpose of trashCount being used like this. i didn’t watch the entire tutorial video, only bits of it.

noted! thnx for the info

yes it’s the implemented interface, but when i changed the void into IEnumerator, some more error pops up.
but after consulting with a lab assistant from my college i found out that i forgot that the coroutine is a function, and i can’t make a function within a function, that was pretty dumb of me haha. that trashCount was my own code to stop the loop
anyway, thanks for your reply!