JoeyObee
JoeyObee

Reputation: 21

yield WaitForSeconds throwing errors

So I'm just attempting to write a script for a game that is meant to destroy the object its attached to after 4 seconds.

But there's a problem with my code that I cant seem to fix. I would really appreciate it if someone could help.

Here's what I have:

public class laserDestroy : MonoBehaviour
{

    void Start ()
    {
        run();
    }

    IEnumerator run()
    {
        yield return new WaitForSeconds(4);
        Destroy(this.GameObject);
    }
}

Upvotes: 0

Views: 290

Answers (1)

Lokkij
Lokkij

Reputation: 424

To start a coroutine, use the StartCoroutine function:

void Start()
{
    StartCoroutine(Run());
    // Alternatively: StartCoroutine("Run")
}

IEnumerator Run()
{
    yield return new WaitForSeconds(4);
    // Code
}

Upvotes: 1

Related Questions