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 RKM_91 · Apr 09, 2015 at 07:16 PM · c#timerunity 4.6quiz

How to integrate timer into my C# script?

Hello Unity Answers,

I'm looking for some help on how to create a timer within my script which appears on screen when the player collides into a cube.

At the moment when the player collides into the cube a question appears on screen. 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.

Any ideas/help/suggestions would be much appreciated.

 using System;
 using System.Collections.Generic;
 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class Question1 : MonoBehaviour {
     
     private Rect windowRect = new Rect (500, 100, 400, 200); //Window size
     public bool question1;
     private int count;
     public Text countText;
 
 
     void start()
     {
         count = 0;
         SetCountText ();
     }
 
     
     void OnGUI()
     {
         windowRect = GUI.Window (0, windowRect, WindowFunction, "Ebola Quiz Island"); //window on screen
     }
     
 
     void WindowFunction (int windowID) 
     {
         // Draw any Controls inside the window here
 
         GUI.Label (new Rect (30, 25, 200, 50), " What year did Ebola begin?"); // Question
         
         if (GUI.Button (new Rect (20, 100, 100, 30), "1976")) // Correct Answer
         {
             Destroy (this.gameObject);
             count = count + 1;
             SetCountText ();
         } 
 
         if (GUI.Button (new Rect (280, 100, 100, 30), "1986")) //Wrong answer  
         {
             Destroy (this.gameObject);
             Application.LoadLevel(Application.loadedLevel);
         }
 
         if (GUI.Button (new Rect (20, 150, 100, 30), "1996")) // wrong answer
         {
             Destroy (this.gameObject);
             Application.LoadLevel(Application.loadedLevel);
         }
 
         if (GUI.Button (new Rect (280, 150, 100, 30), "1966")) // wrong answer
         {
             Destroy (this.gameObject);
             Application.LoadLevel(Application.loadedLevel);
         }
     }
 
     void SetCountText()
         {
             countText.text = "Score: " + count.ToString ();
         }
 }


Player collides with cube and question box appears below :)

alt text

I would like a timer to appear when this question box is on screen and counts down from 10 to 0, with 0 destroying the questionbox and making the player restart the game. :)

alt text

Comment
Add comment · Show 1
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 Pharaoh_ · Apr 09, 2015 at 07:24 PM 0
Share

Set the timer's value to 10f (float). Either use an update() method and check whether a bool is true, which you set to true oncollisionenter() or use an IEnumerator and a while loop within (while (timer > 0)). In this loop, timer -= * Time.deltaTime. Outside of the loop, set another bool to false ("renderBox"). This bool deter$$anonymous$$es whether the GUI element will be rendered in the OnGUI() function.

 void OnGUI() {
 if (renderBox) {
 windowRect = GUI.Window (0, windowRect, WindowFunction, "Ebola Quiz Island");
 }
 }

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Neamtzu · Apr 09, 2015 at 07:25 PM

You can use something like this:

 using UnityEngine;
 using System.Collections;
 
 public class timer : MonoBehaviour {
     private bool showTimer = false; //make it true when you want to display the timer
     private float time = 10f;
     private string timerText;
     
     void Update() {
         if(showTimer) {
             time -= Time.deltaTime;
 
             if(time < 0) {
                 time = 0;
                 showTimer = false;
             }
             
             var seconds = time % 60;//Use the euclidean division for the seconds.
             var fraction = (time * 100) % 100;
             
             //update the label value
             timerText = string.Format ("{0:00} : {1:000}", seconds, fraction);
         }
     }
 }
 

You can get everything that is in Update and add it in your script. When you need to display the timer, make the showTimer variable true. In OnGUI, if showTimer is true, make a label to display the value stored in timerText. It should display the time like this: 09:123 In update, under if(time < 0) you can distroy the object.

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 RKM_91 · Apr 10, 2015 at 11:58 AM

Thank you both for your help managed to get it working. :)

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

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

Related Questions

C# simple timer? 2 Answers

Multiple Cars not working 1 Answer

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

Distribute terrain in zones 3 Answers

Timescale not affect timer? (C#) 2 Answers


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