Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Don't miss out on the Unite LA Keynote for the latest information from Unity! Set a reminder!
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

WaitForSeconds() doesn’t work

Discussion in 'Scripting' started by Bonfry98, Aug 12, 2018.

  1. Bonfry98

    Bonfry98

    Joined:
    Jul 18, 2013
    Posts:
    1
    Hi guys I have a problem with WaitForSeconds that doesn’t work and all the code after it doesn’t execute. Any Ideas. Thanks

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class BallController : MonoBehaviour {
    5. new Rigidbody2D rigidbody2D;
    6. public int ballSpeed = 100;
    7. // Use this for initialization
    8. void Start () {
    9. rigidbody2D = GetComponent<Rigidbody2D>();
    10. transform.position = new Vector2(0f, 0f);
    11. transform.position = new Vector2(0f, 0f);
    12. StartCoroutine(Wait(2));
    13. StartBall();
    14. }
    15. public void StartBall()
    16. {
    17. float randomNum = Random.Range(0, 2);
    18. if (randomNum <= 0.5)
    19. rigidbody2D.AddForce(new Vector2(ballSpeed, 10));
    20. else
    21. rigidbody2D.AddForce(new Vector2(-ballSpeed, -10));
    22. }
    23. public void ResetBall()
    24. {
    25. transform.position = new Vector2(0f, 0f);
    26. rigidbody2D.velocity = new Vector2(0f, 0f);
    27. StartCoroutine(Wait(1));
    28. StartBall();
    29. }
    30. public IEnumerator Wait(float time)
    31. {
    32. print(Time.time);
    33. yield return new WaitForSeconds(5);
    34. print(Time.time);
    35. }
    36. private void Update()
    37. {
    38. float xVel = rigidbody2D.velocity.x;
    39. if (xVel < 18 && xVel > -18 && xVel != 0)
    40. {
    41. if (xVel > 0)
    42. rigidbody2D.velocity = new Vector2(20, rigidbody2D.velocity.y);
    43. else
    44. rigidbody2D.velocity = new Vector2(-20, rigidbody2D.velocity.y);
    45. }
    46. }
    47. // Update is called once per frame
    48. private void OnCollisionEnter2D(Collision2D collision)
    49. {
    50. if(collision.collider.tag == "Player")
    51. {
    52. rigidbody2D.velocity = new Vector2(
    53. rigidbody2D.velocity.x, rigidbody2D.velocity.y/2 +
    54. collision.collider.GetComponent<Rigidbody2D>().velocity.y/3
    55. );
    56. GetComponent<AudioSource>().pitch = Random.Range(0.7f,1.3f);
    57. GetComponent<AudioSource>().Play();
    58. }
    59. }
    60. }
     
  2. Nick4

    Nick4

    Joined:
    Nov 27, 2013
    Posts:
    51
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class BallController : MonoBehaviour {
    5.     new Rigidbody2D rigidbody2D;
    6.     public int ballSpeed = 100;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.         rigidbody2D = GetComponent<Rigidbody2D>();
    11.         transform.position = new Vector2(0f, 0f);
    12.         transform.position = new Vector2(0f, 0f);
    13.         StartCoroutine(Wait(2));
    14.         StartBall();
    15.     }  
    16.  
    17.     public void StartBall() {
    18.         float randomNum = Random.Range(0, 2);
    19.         if (randomNum <= 0.5)
    20.             rigidbody2D.AddForce(new Vector2(ballSpeed, 10));
    21.         else
    22.             rigidbody2D.AddForce(new Vector2(-ballSpeed, -10));
    23.     }
    24.  
    25.     public void ResetBall() {
    26.         transform.position = new Vector2(0f, 0f);
    27.         rigidbody2D.velocity = new Vector2(0f, 0f);
    28.         StartCoroutine(Wait(1));
    29.         StartBall();
    30.     }
    31.  
    32.     public IEnumerator Wait(float time) {
    33.         print(Time.time);
    34.         yield return new WaitForSeconds(5);
    35.         print(Time.time);
    36.     }
    37.  
    38.     private void Update() {
    39.         float xVel = rigidbody2D.velocity.x;
    40.         if (xVel < 18 && xVel > -18 && xVel != 0) {
    41.             if (xVel > 0)
    42.                 rigidbody2D.velocity = new Vector2(20, rigidbody2D.velocity.y);
    43.             else
    44.                 rigidbody2D.velocity = new Vector2(-20, rigidbody2D.velocity.y);
    45.         }
    46.     }
    47.  
    48.     private void OnCollisionEnter2D(Collision2D collision) {
    49.         if(collision.collider.tag == "Player") {
    50.             rigidbody2D.velocity = new Vector2(
    51.             rigidbody2D.velocity.x, rigidbody2D.velocity.y/2 +
    52.             collision.collider.GetComponent<Rigidbody2D>().velocity.y/3);
    53.             GetComponent<AudioSource>().pitch = Random.Range(0.7f,1.3f);
    54.             GetComponent<AudioSource>().Play();
    55.         }
    56.     }
    57. }
    Please make sure your code is tidy & readable next time.
    And you're not using the "time" variable that you're passing as a parameter in your Wait(float time) function, maybe that's it?

    Code (CSharp):
    1.     public IEnumerator Wait(float time) {
    2.         print(Time.time);
    3.         yield return new WaitForSeconds(5); /* yield return new WaitForSeconds(time); */
    4.         print(Time.time);
    5.     }
     
  3. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    1,720
    Your coroutine doesn't actually do anything, as presented. If you're trying to get your other functions to wait for the Wait function to finish, you need to also make those coroutines and call:
    Code (csharp):
    1.  
    2. yield return StartCoroutine(Wait(however long));
    3.  
    Right now all the functions are doing is starting a new coroutine and then carrying on their merry way.