# How to stop a function and start a countdown in a 2D game?

Hi everybody !

I’m making a little game with 2D objects (box, square, circles, etc…) appearing when I click. I have this code actually making things work : everytime I use the mouseclick a Gameobject is created from the array list in the inspector.

``````    //Changed to an array of objects assign as many as you have in inspector
var boxes : GameObject[];
var boxCounter : int;

function Update()
{
if(Input.GetMouseButtonDown(0))//Checks to see if left mouse button was clicked.
{
CreateBox();
}
}

function CreateBox()
{
var mousePos : Vector2 = Input.mousePosition;

//Depth you want the center of the object to be is z which I used zero
var boxPos : Vector3 = camera.ScreenToWorldPoint(mousePos.x, mousePos.y, 0);

//I used the perfab box's rotation here but you can enter what you'd like as a euler using Quaternion.Euler(x,y,z)
Instantiate(boxes[boxCounter], boxPos, boxes[boxCounter].transform.rotation);

//This will increment if there are more boxes or reset to 0 if it is the last one.
if(boxCounter == boxes.length-1)
{
boxCounter = 0;
}else{
boxCounter ++;
}
}
``````

But now I want a countdown to start when all the Gameobject are in the game and no other gameobject to come in the game. Actually the counter restart at 0 and you can put Gameobjects again and again. The idea is, when all the Gameobjects are in the game the countdown starts and after 8 seconds for example it is win. I tried something like if BoxCounter = 0 stop the function but it isn’t working very well. Any other ideas?

void Update()
{

``````float TimeAfterBoxes = 0.0f;
(GameObject.Find("GameObjectWitGUIText")).guiText.text = "CountDown : " + TimeAfterBoxes; // This will show the Countdown time
if(boxCounter == boxes.length-1)//Based on No. of Boxes You need.
{
TimeAfterColliding += Time.deltaTime;
if( TimeAfterColliding > 8.00f)
{  WIN  Concept HERE ;   }
}
``````

You can use Time.FixedDeltaTime instead of Time.deltaTime for Speed Update.