I have had excruciating tries of finding a solution to this error, but I seem to find nothing. I have a simple cube prefab that spawns, and I want it to be destroyed after a three second delay. I used this:
I am getting the same stupid error over and over, no matter what I do. Here it is:
Can anyone help me out? Thanks in advance!
asked Jul 24, 2013 at 02:16 PM
yield can only be used inside an Iterator method. An iterator method is best invoked using a corouting.
Try something like this:
The above code will not totally solve your particular problem, however, because you have other problems with the way you are instantiating your objects and such. When you Destroy (gameObject) you are basically destroying whatever object the script is on, not the one that got created during Instatiate. I didn't solve that for you ;-).
Having said all that I was basically answering your question about the specific problem you are having. If it were me I would not use the coroutine method at all. Destroy has a time delay parameter right on it. You just have to use: Destroy (gameObject, x) where x is the number of seconds to delay before destroying. In your case it would be Destroy (gameObject, 3);
answered Jul 24, 2013 at 02:25 PM