1

I am new to Unity and C#, I am trying to add a 'wait time' to my death script. So when my Player dies it shows a particle animation and then resets the level, however, at the moment my particles are playing but the level doesn't reset.

public GameObject particles; public Player_Movement player;

void OnCollisionEnter2D(Collision2D Col)
{
    if (Col.collider.tag == "Enemy")
    {
        Instantiate(particles, transform.position, Quaternion.identity);
        Destroy(gameObject);
        StartCoroutine("RestartGameCo");
    }
}
public IEnumerator RestartGameCo()
{
    yield return new WaitForSeconds(0.5f);
    SceneManager.LoadScene("Level1");
}
J L
  • 65
  • 6
  • 1
    *`WaitForSeconds(0.5f)`* Use a larger number? – Draco18s no longer trusts SE May 15 '19 at 01:43
  • @Draco18s I actually dont think its the time, i have used numbers between 0.001 and 100f. Im going to edit my post now but I'm getting the particle effect however the game isnt restarting. Thank you for your reply! – J L May 15 '19 at 01:54

3 Answers3

2
    Destroy(gameObject);
    StartCoroutine("RestartGameCo");

Your code is fine, but you destroy the gameobject that has this script on it. Which also destroys the script, and stops all the coroutines. So it will never be called.

A solution is to make the object invisible in some way, like disabling the mesh renderer and collider instead of destroying it.

HoloLady
  • 1,041
  • 1
  • 12
  • 28
0

I know a couple options about that. Here's one.

By using the Thread, it will pause your application for the amount of time of your desire. To do so, you need to add using System.Threading; to your the current cs file you are editing. Now simply change your code to that:

void OnCollisionEnter2D(Collision2D Col)
{
    Thread.Sleep(5000) // 5000 in milliseconds
    if (Col.collider.tag == "Enemy")
    {
        Instantiate(particles, transform.position, Quaternion.identity);
        Destroy(gameObject);
        StartCoroutine("RestartGameCo");
    }
}
public IEnumerator RestartGameCo()
{
    Thread.Sleep(5000)
    yield return new WaitForSeconds(0.5f);
    SceneManager.LoadScene("Level1");
}
Voxaim
  • 45
  • 4
  • Thank you so much for your reply, I miss wrote my post (its updated now) I am getting the particle effect however my scene wont reset. – J L May 15 '19 at 01:58
  • 2
    Do not use `Threads` with Unity. Terrible terrible idea. Why? Because you can't do things like `SceneManager.LoadScene("Level1");` from anywhere but the main thread. Oh wait, you haven't created a new thread, you just put the main thread to sleep...freezing the whole game. See also [why is thread.sleep so harmful?](https://stackoverflow.com/questions/8815895/why-is-thread-sleep-so-harmful) – Draco18s no longer trusts SE May 15 '19 at 01:58
0

You are actually destroying the gameobject itself before even running the co-routine. It means that the co-routine "RestartGameCo" won't even run. One way to debug these kind of things is using Debug.log messages.

Code:

void OnCollisionEnter2D(Collision2D Col)
{
    if (Col.collider.tag == "Enemy")
    {
        Instantiate(particles, transform.position, Quaternion.identity);
        StartCoroutine("RestartGameCo");
    }
}
public IEnumerator RestartGameCo()
{
    yield return new WaitForSeconds(0.5f);
    SceneManager.LoadScene("Level1");
}

Let me know if it helps.