Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. 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 at 4:40 PM.

  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. StartBall();
    13. }
    14. public void StartBall()
    15. {
    16. float randomNum = Random.Range(0, 2);
    17. if (randomNum <= 0.5)
    18. rigidbody2D.AddForce(new Vector2(ballSpeed, 10));
    19. else
    20. rigidbody2D.AddForce(new Vector2(-ballSpeed, -10));
    21. }
    22. public void ResetBall()
    23. {
    24. transform.position = new Vector2(0f, 0f);
    25. rigidbody2D.velocity = new Vector2(0f, 0f);
    26. StartBall();
    27. }
    28. public IEnumerator Wait(float time)
    29. {
    30. print(Time.time);
    31. yield return new WaitForSeconds(5);
    32. print(Time.time);
    33. }
    34. private void Update()
    35. {
    36. float xVel = rigidbody2D.velocity.x;
    37. if (xVel < 18 && xVel > -18 && xVel != 0)
    38. {
    39. if (xVel > 0)
    40. rigidbody2D.velocity = new Vector2(20, rigidbody2D.velocity.y);
    41. else
    42. rigidbody2D.velocity = new Vector2(-20, rigidbody2D.velocity.y);
    43. }
    44. }
    45. // Update is called once per frame
    46. private void OnCollisionEnter2D(Collision2D collision)
    47. {
    48. if(collision.collider.tag == "Player")
    49. {
    50. rigidbody2D.velocity = new Vector2(
    51. rigidbody2D.velocity.x, rigidbody2D.velocity.y/2 +
    52. collision.collider.GetComponent<Rigidbody2D>().velocity.y/3
    53. );
    54. GetComponent<AudioSource>().pitch = Random.Range(0.7f,1.3f);
    55. GetComponent<AudioSource>().Play();
    56. }
    57. }
    58. }
     
  2. Nick4

    Nick4

    Joined:
    Nov 27, 2013
    Posts:
    48
    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,580
    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.