Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

WaitforSeconds before Spawning (Proper use) [SOLVED]

Discussion in 'Scripting' started by Troas, Jun 23, 2014.

  1. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157
    So I'm trying to make the spawn mechanism wait before it respawns the player using the following code so far:

    Code (CSharp):
    1. void Die (){
    2.         Instantiate(deathParticles, transform.position, Quaternion.Euler (270,0,0));
    3.         yield return new WaitForSeconds(4);
    4.         transform.position = spawn;
    5.     }
    However it gives me the error "Assets/Scripts/PlayerMovement.cs(51,14): error CS1624: The body of `PlayerMovement.Die()' cannot be an iterator block because `void' is not an iterator interface type"

    I'm still pretty new to C# so some help understanding what all this means would be great.
     
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    The StartCoroutine requires your function to be of type IEnumerator, not void.
     
  3. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157
    Code (CSharp):
    1. IEnumerator Die (){
    2.         Instantiate(deathParticles, transform.position, Quaternion.Euler (270,0,0));
    3.         yield return new WaitForSeconds(4);
    4.         transform.position = spawn;
    5.     }
    That all good?

    Can you link me something that gives me a sort of crash course for IEnumerators btw? (Besides the Unity video)
     
  4. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Code (csharp):
    1.  
    2. transform.position= spawn;
    3.  
    how does this spawn the player?
     
  5. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157
    It doesn't, that's another part of the script that isn't needed to be shown because it works. This just sets the position of the player to the spawn.
     
  6. Sharp-Development

    Sharp-Development

    Joined:
    Nov 14, 2013
    Posts:
    353
  7. daminhtung

    daminhtung

    Joined:
    Jan 5, 2013
    Posts:
    10