Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ziakhatri · Jun 05, 2011 at 10:43 AM · javascripttimedelay

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(); }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges