- Home /
Ball throw and brick count
I want to throw 10 balls maximum and have 3 bricks to hit. In this script when I have thrown 9 balls and only 1 brick is remaining, as soon as the last ball is thrown the counter checks for 10 balls thrown and "GAME OVER" message is printed before the ball hits the last brick. If last brick falls then the message "YOU WIN" is printed. I want to delay Balls_count ++ by 2-3 seconds so that the ball can hit the brick and then Balls_count go from 9 to 10.
I want to delay ball throw by 1-2 seconds too. Please help:
Following Javascript is used with the error:
BThrow.js
static var Balls_count : int = 0; static var totalbricks : int = 0; var speed = 2.0; var ballPrefab : Transform; function brickcount(){ var brickx1 : int = 0; var brickx4 : int = 0; var brickx5 : int = 0; brickx1 = pbrick1.brick1; brickx4 = pbrick4.brick4; brickx5 = pbrick5.brick5; totalbricks = brickx1+brickx4+brickx5;
if(totalbricks == 0){ print("WON"); } Debug.Log (totalbricks+" - " +Balls_count);
if(Balls_count < 10){ if(Input.GetButtonDown("Fire1")) { var ball = Instantiate(ballPrefab, transform.position, Quaternion.identity); ball.rigidbody.AddForce(transform.forward * 2000);
if(totalbricks==0){ print("YOU WIN"); } if(totalbricks > 0) { print("YOU LOSE"); } Balls_count ++; }
function LateUpdate () { brickcount(); }
Answer by aldonaletto · Jun 05, 2011 at 02:38 PM
Your script seems to be incomplete - there are two missing closing curly braces.
Declare a delay variable outside any function:
private var t1:float = 0;
Start the delay when firing the ball:
if (Input.GetButtonDown("Fire1")){
t1 = Time.time+2.5; // 2.5s delay
var ball = Instantiate(...
Use the t1 variable to suspend brickcount() during delay time:
function LateUpdate(){
if (Time.time>t1) brickcount();
}
Anyway, I think using delays is not the right approach - the delay may become too short or too long depending on the screen size. You should instead check the Game Over condition only when the ball has no more chances to hit any brick. You can put a trigger cube after the bricks (for instance) in order to detect when the balls passed the bricks, and check brickCount only in the OnTriggerEnter function.
Your answer
![](https://koobas.hobune.stream/wayback/20220612095510im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Using Time with OnGUI Help 2 Answers
Time Function in Javascript? 1 Answer
How to make GUI Text appear after a certain amount of time 2 Answers
Creating delays without delaying the script 2 Answers
Subtract time from time 1 Answer