x


WaitForSeconds...

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:


using UnityEngine;
using System.Collections;

public class Cubes : MonoBehaviour {

    public float delay = 0.1f;
    public GameObject cube;
    public int destroyTimer = 3;

    void Start () {
       InvokeRepeating("Spawn", delay, delay);
       Destruction();
    }

    void Spawn () {
       Instantiate(cube, new Vector3(Random.Range(-6, 6), 10, -2), Quaternion.identity);
    }

    void Destruction() {
       yield return new WaitForSeconds(destroyTimer);
       Destroy(gameObject);
    }

}

I am getting the same stupid error over and over, no matter what I do. Here it is: "Assets/Cubes.cs(18,14): error CS1624: The body ofCubes.Destruction()' cannot be an iterator block because void' is not an iterator interface type"

Can anyone help me out? Thanks in advance!

more ▼

asked Jul 24 '13 at 02:16 PM

DubstepDragon gravatar image

DubstepDragon
558 22 51 63

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Use the variant with a delay.

Destroy(gameObject, destroyTimer);
more ▼

answered Jul 24 '13 at 02:27 PM

vividhelix gravatar image

vividhelix
342 1 4 10

Awesome, thanks a bunch!

Jul 24 '13 at 02:28 PM DubstepDragon
(comments are locked)
10|3000 characters needed characters left

yield can only be used inside an Iterator method. An iterator method is best invoked using a corouting.

Try something like this:

IEnumerator DestroyRouting ()
{
  yield return new WaitForSeconds (destroyTimer);
  Destroy (gameObject);
}

void Destruction ()
{
  StartCorouting (DestroyRouting ());
}

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);

more ▼

answered Jul 24 '13 at 02:25 PM

Hotshot10101 gravatar image

Hotshot10101
367 21 37 43

That is a possibility, but radlemur's answer is much easier and works. Thanks anyway dude!

Jul 24 '13 at 02:28 PM DubstepDragon

If you look at END of my answer I said the same thing as he did. I was just answering your other question about why you were getting the error and how to use it correctly.

Jul 24 '13 at 02:30 PM Hotshot10101

I missed that too, otherwise I wouldn't have posted the same thing. When I saw the IEnumerator code I stopped reading.

Jul 24 '13 at 02:34 PM vividhelix
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5510
x2984
x461
x239
x1

asked: Jul 24 '13 at 02:16 PM

Seen: 317 times

Last Updated: Jul 24 '13 at 02:34 PM