# My coroutine does not wait

Hi i’m making a pong clone and i need a timer to wait when the ball stays at the center, the when the timer run out of time i shoot the ball in front of the player’s paddle, i tryied everything and searched both in the documentation and the answers.unity.com but still the coroutine isn’t working so i paste the code of the script here:

``````using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ball_script : MonoBehaviour
{

public float startingForce;
public float minY, maxY;
public float maxXVelocity;
public float actualvelX = 0;

private int angle;
private Vector2 position;
private Rigidbody2D rb;
private Vector2 actualVelocity;

// Use this for initialization
void Start ()
{
rb = GetComponent<Rigidbody2D>();

LaunchBall();
}

void FixedUpdate()
{
actualVelocity = rb.velocity;
}

void OnCollisionEnter2D(Collision2D col)
{

if (col.gameObject.tag=="wallup")
{
rb.velocity = new Vector2(actualVelocity.x,-actualVelocity.y);
}
{
actualVelocity.x= actualVelocity.x < 0 ? actualVelocity.x -= 0.2f : actualVelocity.x += 0.5f;

rb.velocity = new Vector2(-Mathf.Clamp(actualVelocity.x,actualVelocity.x,maxXVelocity), actualVelocity.y);
actualvelX = actualVelocity.x;
}
else
{
LaunchBall();
}
}

void LaunchBall()
{

position = new Vector2(1, 1); //Get the ball position
rb.MovePosition(Vector2.zero); //Puts the ball above the center
StartCoroutine(WaitLaunch());
position += new Vector2(-(position.x * startingForce), Random.Range(-position.y * 2, position.y * 2)); //Modify position variable to launch ball
rb.velocity = position; //Launch the ball

}

IEnumerator WaitLaunch()
{
yield return new WaitForSeconds(4.0F);
}
}
``````

Little note: i used coroutine to do the same exact thing and in previous versions of unity it was working…i’m quite confused

Coroutine runs code in an asynchronous matter (or rather, it’s “paused” when you call yield return and returned to after the stated time (i.e. WaitForEndOfFrame, WaitForSeconds, etc), I believe Unity internally uses a List/Dictionary of coroutines and decrement the timer every frame).

StartCoroutine() just says “Hey, do this thing in the background (not really background, still the main thread)”. It does not stop the completion of the function it’s called in (when you use yield return you return control to the function which called it).

What you need to to is make LaunchBall() a coroutine as well and then use “yield return StartCoroutine(WaitLaunch())”. This basically says: “Wait until the stated function is finished before you continue with this function”.