How to add wait time between death and level reset. c#

Related searches

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

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.

restart level when dead with delay, Hello guys, i'm struggling with something. after my player loses all his lives, i want him to restart the level with a delay i did add a yield but i� Once it has been signaled, ManualResetEvent remains signaled until it is manually reset by calling the Reset() method. That is, calls to WaitOne return immediately. You can control the initial state of a ManualResetEvent by passing a Boolean value to the constructor: true if the initial state is signaled, and false otherwise.

    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.

Initiate a restart of the level after character death, Initiate a restart of the level after character death I'm new to C#. I'm kind of moving over from java script so I don't have reference for how to do this. Add comment � Show 2 set time you want to wait after death. This time� Suspends the coroutine execution for the given amount of seconds using scaled time. The real time suspended is equal to the given time divided by Time.timeScale. See WaitForSecondsRealtime if you wish to wait using unscaled time. WaitForSeconds can only be used with a yield statement in coroutines.

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

Adding Pause, Main Menu and Game over Screens in Unity, Vincent Quarles builds on a previously written tutorial by adding a pause, to switch between levels and activate certain controls (such as pausing the UI elements only at certain times – such as when the game is paused or to use to restart a level, move back to the main menu, or un-pause the game. yield WaitForSeconds (time in seconds) used for delays. But I can't use it with Function Update or Function LateUpdate. And "coroutine" function let me use "yield" deadManNs code is hard for me to understand right now. I will try to figure out it as well. Thanks for your help.

Death and Respawning, Game Programming with Unity and C# pp 173-180 | Cite as their model will disappear, and the Player script will be disabled to keep it from updating. After a few seconds of wait (to punish them for failing), we'll move them dead, a bool depicting whether the player is alive (false) or dead (true) at the time. The actual time delay may vary from the time specified in time_to_pass, time_to_execute, or timeout, and depends on the activity level of the server. The time counter starts when the WAITFOR statement thread is scheduled. If the server is busy, the thread may not be immediately scheduled, so the time delay may be longer than the specified time.

Availability group lease health check timeout, The resource monitor polls IsAlive at the cluster heartbeat interval, which is set but the cluster this time does not wait for the resource DLL to report success or prevent lease renewal from the SQL instance and causing a restart or failover. Level, Condition under which the instance is considered dead� When you execute the first program, you will notice that the program does not require significant CPU time when executing the Thread.Sleep calls. Important: The program only starts consuming the entire CPU core when the Thread.SpinWait method is invoked.

time: The time at the beginning of this frame (Read Only). This is the time in seconds since the start of the game. timeScale: The scale at which time passes. This can be used for slow motion effects. timeSinceLevelLoad: The time this frame has started (Read Only). This is the time in seconds since the last level has been loaded. unscaledDeltaTime

Comments
  • WaitForSeconds(0.5f) Use a larger number?
  • @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!
  • Thank you very much, this has helped massively!
  • Thank you so much!
  • 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.
  • 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?