Search Unity

WaitForSecondsRealtime(5); WaitForSeconds , coroutine

Discussion in 'Scripting' started by ChuckieGreen, Nov 24, 2017.

  1. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    358
    So i am using this function for my player dying

    Code (CSharp):
    1. void DeathGoTo()
    2.     {
    3.         if (death == true)
    4.         {
    5.            
    6.             SceneManager.LoadScene(2);
    7.         }
    8.     }
    But I want to use WaitForSeconds to wait a few seconds before changing scenes, but i get the suspends the coroutine execution , cannot be used as a method error, I get that for both WaitForSecondsRealtime and WaitForSeconds , is there anyway i can use this in the code ive shown?
     
  2. Kaart

    Kaart

    Joined:
    Jul 31, 2017
    Posts:
    62
    To wait a few seconds before switchting scene change your change your DeathGoTo() function a little:
    Code (CSharp):
    1.     IEnumerator DeathGoTo()
    2.     {
    3.         yield return new WaitForSeconds(5); // Amount of seconds to wait.
    4.         SceneManager.LoadScene(2);
    5.     }
    Call it like this:
    Code (CSharp):
    1. StartCoroutine(DeathGoTo());