Reputation: 21
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
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