Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Midknight51 · Feb 11, 2014 at 02:39 AM · deltatimegetkeywhile loop

While loop timer stops when button is pressed.

Good Evening, I am trying to create a script that will countdown from 5. Before the countdown, the script will wait a random number of seconds before saying "Go!". During this countdown, if the user presses the key "x" before the timer has finished counting down then the user wins. However, if the user does not press x before the countdown completes, the computer wins. I tried doing this in a while loop but it didn't work. The variable seconds will almost immediately drop to below 0 setting cpuWin to true.

 var cpuWin : boolean = false;
 var playerWin : boolean = false;
 var seconds : float = 5.0;
 
 function Start () {
     yield WaitForSeconds(Random.Range(1,10));
     Debug.Log("Go!");
     
     while(seconds > 0) {
         if(Input.GetKey("x")) {
             playerWin = true;
             Debug.Log("You win!");
             break;
         }
         
         seconds -= Time.deltaTime;
         
         if(seconds <= 0) {
             cpuWin = true;
             Debug.Log("You lose!");
             break;
         }
     }
     
     //Once out of the loop do other stuff.
 }

Sorry if im missing something here. Im getting back into programming before I go back to school and my logic and coding is very rough. Am I at least headed in the right direction? Or is there an easier way to do this?

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
1
Best Answer

Answer by AdamScura · Feb 11, 2014 at 03:20 AM

What you're missing is how Unity handles the main game loop.

You don't make the main loop yourself in a script - Unity handles the main loop for you automatically.

Put your startup code in the "Start" function. Put the code that executes every frame into the "Update" function. Unity will automatically call these functions on every active script in your scene at the appropriate time.

Here is a great info-graphic that shows the life-cycle for a Unity script. You can implement any of those functions to hook your code into the main loop and it will be executed automatically.

It probably wouldn't hurt to read through the unity manual as well.

A delay can be done in unity by creating a Coroutine.

Comment
Add comment · Show 1 · 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
avatar image Midknight51 · Feb 11, 2014 at 01:10 PM 0
Share

Thanks for that infographic! It should come in handy. It looks like im going to need some more training before I really begin doing things myself. I'm going to go through some of the tutorials since I dont understand how Unity works quite yet. Im used to typing it all myself, the fact that Unity does it for you is great, I just have to figure out how to get it to work for me. Thanks again!

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

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity While Loop Freeze 2 Answers

Audio trigger by key press - problems 1 Answer

Problems with GetKeyUp generating text outside Unity 1 Answer

Input.GetKey not working 1 Answer

Is SimpleMove() framerate-independent under the hood? 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