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

Time Delay not working

Discussion in 'Scripting' started by left4kill5, Sep 17, 2020.

  1. left4kill5

    left4kill5

    Joined:
    Sep 17, 2020
    Posts:
    12
    Hello, I need some help. I was trying to figure out how Time Delay worked and I think I did it correctly. However, a few error signs pop up. Can you please explain them to me and how to fix? I am new at programming, so I spend most of my time trying to find tutorials and debug stuff.

    Script:
    using UnityEngine;
    using UnityEngine.SceneManagement;

    public class Playercollision : MonoBehaviour
    {

    public Playermovement movement;
    public Jumpingmechanic jump;
    public Rigidbody rb;

    void OnCollisionEnter (Collision collision)
    {

    iEnumerator Timedelay();
    {


    yield return new WaitForSeconds(3);

    SceneManager.LoadScene("Gamemover scene");
    }

    if (collision.collider.tag == "Obstacle")
    {
    movement.enabled = false;
    StartCoroutine(Timedelay());

    }


    if (collision.collider.tag == "jump")
    {

    jump.enabled = true;

    }

    if (collision.collider.tag == "recovery")
    {

    movement.enabled = true;
    rb.drag = 2;
    jump.enabled = false;


    }
    }
    }

    My errors:
    1.
    Assets\Scripts\Playercollision.cs(14,14): error CS8112: 'Timedelay()' is a local function and must therefore always have a body.

    2.
    Assets\Scripts\Playercollision.cs(14,2): error CS0246: The type or namespace name 'iEnumerator' could not be found (are you missing a using directive or an assembly reference?)

    3.
    Assets\Scripts\Playercollision.cs(11,10): error CS1624: The body of 'Playercollision.OnCollisionEnter(Collision)' cannot be an iterator block because 'void' is not an iterator interface type

    They make no sense because when I remove the Bracket at line 21, the errors have been replaced with
    Assets\Scripts\Playercollision.cs(48,2): error CS1513: } expected.
    Is this normal? I have no idea. Thanks!
     

    Attached Files:

  2. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    That should fix it:

    you need to create a function IEnumerator,
    you cannot create a function inside another :)

    Code (CSharp):
    1. public class Playercollision : MonoBehaviour
    2. {
    3.  
    4.     public Playermovement movement;
    5.     public Jumpingmechanic jump;
    6.     public Rigidbody rb;
    7.  
    8.     public IEnumerator LoadSceneWithDelay(float time)
    9.     {
    10.         yield return new WaitForSeconds(time);
    11.         SceneManager.LoadScene("Gamemover scene");
    12.     }
    13.  
    14.     void OnCollisionEnter (Collision collision)
    15.     {
    16.  
    17.  
    18.         StartCoroutine(LoadSceneWithDelay(5));
    19.  
    20.          
    21.         if (collision.collider.tag == "Obstacle")
    22.         {
    23.             movement.enabled = false;
    24.             StartCoroutine(Timedelay());
    25.          
    26.         }
    27.      
    28.    
    29.         if (collision.collider.tag == "jump")
    30.         {
    31.          
    32.             jump.enabled = true;
    33.  
    34.         }
    35.      
    36.         if (collision.collider.tag == "recovery")
    37.         {
    38.          
    39.             movement.enabled = true;
    40.             rb.drag = 2;
    41.             jump.enabled = false;
    42.  
    43.          
    44.         }
    45.       }  
    46. }
     
  3. left4kill5

    left4kill5

    Joined:
    Sep 17, 2020
    Posts:
    12
    Thanks! I think I've got it.