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 /
This question was closed Apr 26, 2015 at 08:18 PM by RKM_91 for the following reason:

Other

avatar image
2
Question by RKM_91 · Apr 04, 2015 at 03:21 PM · c#timerunity 4.6quiz

C# simple timer?

Hi,

I want to create a timer which will appear on screen when the player has collided in to a cube which at the moment brings up a pop up box displaying a question.

I would like to display a timer counting down from 10 to 0, with 0 destroying the pop up box and the player going back to the start.

At the moment if the player gets the question wrong, the player will go back to the start but has a long time to answer the question so would like to limit this with a timer. :)

I really can't think of a way to do this off the top of my head, so if anyone can either post some code or point me in the right direction, that would be awesome. :)

alt text

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

2 Replies

  • Sort: 
avatar image
3

Answer by hahahaha · Apr 04, 2015 at 05:15 PM

Hey, I'd do it with a bool, and this simple Timer script.

 public bool PopUpboxActive;
     
     public float Timer = 10f;
     
     
     void OnCollisionEnter(Collision col) {
     
         //Keep whatever script you have here for Showing the popupbox but also add or incoporate the following...
     
         PopUpboxActive = true;
     
     }
     
     
     void FixedUpdate() {
     
         if(PopUpboxActive == true) {
             Timer -= Time.deltaTime;
         }
     
     
         if(Timer <= 0f) {
             PopUpboxActive = false;
             Timer = 10f; //resets timer for next time;
     
             //You can send the player back to the start here if you want
     
         }  
     }
 
 
 Void OnGUI() {
 
     if (PopUpboxActive == true) {
 
         GUI.Label (new Rect (100, 100, 400, 50), "Time Left to Answer Question" + Timer.ToString("F0")); //This will display the timer when the Popupbox is active, the "F0" part rounds the number to 0 decimal places, if you want to include decimals, change the number to 1 or 2 depending on how many decimal points :)
     }
 
 }

Hope this helps! :)

Sam

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
avatar image
0

Answer by Veldars · Apr 04, 2015 at 03:37 PM

You can use a Text3D

 private Text timerTxt;
 
 void Start() {
   // Get the text object in your scene
   timerTxt = GameObject.FindGqmeObjectWithTag("Timer").GetComponent< Text >();

  // Start the timer
   StartCoroutine(StartTimer());
 }
 
 IEnumerator StartTimer(){
 
   int timer = 10;
   while (timer > 0) {
     // Draw the resting time
     timerTxt.Text = timer.ToString();

     yield WaitForSeconds (1);

     // Decrease the timer
     timer--;
   }
   
   // You can call here your fail function...
   PlayerFail();
 }


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

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to integrate timer into my C# script? 2 Answers

the countdown timer inside a spawn() and inside while loop not working 1 Answer

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Start timer on mouse0 click 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