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): using System.Collections; using System.Collections.Generic; using UnityEngine; public class BallController : MonoBehaviour { new Rigidbody2D rigidbody2D; public int ballSpeed = 100; // Use this for initialization void Start () { rigidbody2D = GetComponent<Rigidbody2D>(); transform.position = new Vector2(0f, 0f); transform.position = new Vector2(0f, 0f); StartCoroutine(Wait(2)); StartBall(); } public void StartBall() { float randomNum = Random.Range(0, 2); if (randomNum <= 0.5) rigidbody2D.AddForce(new Vector2(ballSpeed, 10)); else rigidbody2D.AddForce(new Vector2(-ballSpeed, -10)); } public void ResetBall() { transform.position = new Vector2(0f, 0f); rigidbody2D.velocity = new Vector2(0f, 0f); StartCoroutine(Wait(1)); StartBall(); } public IEnumerator Wait(float time) { print(Time.time); yield return new WaitForSeconds(5); print(Time.time); } private void Update() { float xVel = rigidbody2D.velocity.x; if (xVel < 18 && xVel > -18 && xVel != 0) { if (xVel > 0) rigidbody2D.velocity = new Vector2(20, rigidbody2D.velocity.y); else rigidbody2D.velocity = new Vector2(-20, rigidbody2D.velocity.y); } } // Update is called once per frame private void OnCollisionEnter2D(Collision2D collision) { if(collision.collider.tag == "Player") { rigidbody2D.velocity = new Vector2( rigidbody2D.velocity.x, rigidbody2D.velocity.y/2 + collision.collider.GetComponent<Rigidbody2D>().velocity.y/3 ); GetComponent<AudioSource>().pitch = Random.Range(0.7f,1.3f); GetComponent<AudioSource>().Play(); } } }
Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class BallController : MonoBehaviour { new Rigidbody2D rigidbody2D; public int ballSpeed = 100; // Use this for initialization void Start () { rigidbody2D = GetComponent<Rigidbody2D>(); transform.position = new Vector2(0f, 0f); transform.position = new Vector2(0f, 0f); StartCoroutine(Wait(2)); StartBall(); } public void StartBall() { float randomNum = Random.Range(0, 2); if (randomNum <= 0.5) rigidbody2D.AddForce(new Vector2(ballSpeed, 10)); else rigidbody2D.AddForce(new Vector2(-ballSpeed, -10)); } public void ResetBall() { transform.position = new Vector2(0f, 0f); rigidbody2D.velocity = new Vector2(0f, 0f); StartCoroutine(Wait(1)); StartBall(); } public IEnumerator Wait(float time) { print(Time.time); yield return new WaitForSeconds(5); print(Time.time); } private void Update() { float xVel = rigidbody2D.velocity.x; if (xVel < 18 && xVel > -18 && xVel != 0) { if (xVel > 0) rigidbody2D.velocity = new Vector2(20, rigidbody2D.velocity.y); else rigidbody2D.velocity = new Vector2(-20, rigidbody2D.velocity.y); } } private void OnCollisionEnter2D(Collision2D collision) { if(collision.collider.tag == "Player") { rigidbody2D.velocity = new Vector2( rigidbody2D.velocity.x, rigidbody2D.velocity.y/2 + collision.collider.GetComponent<Rigidbody2D>().velocity.y/3); GetComponent<AudioSource>().pitch = Random.Range(0.7f,1.3f); GetComponent<AudioSource>().Play(); } } } 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): public IEnumerator Wait(float time) { print(Time.time); yield return new WaitForSeconds(5); /* yield return new WaitForSeconds(time); */ print(Time.time); }
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): yield return StartCoroutine(Wait(however long)); Right now all the functions are doing is starting a new coroutine and then carrying on their merry way.