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 /
  • Help Room /
avatar image
0
Question by MartSeishuuin_29 · Aug 29, 2015 at 12:46 PM · score systemtimer countdown

how to get remaining time in a count down timer as a score?

Hello i'm new at using unity and i'm making a game, please I want to know how to get the remaining value of the timer and get it as a score. I have a treasure chest that when the player collides with it a GUI will appear and there's a text (the question) and 4 buttons (multiple choices) with a timer of 10 seconds that counts down to 0. (ex. If the player answered correct with a remaining time of 5 he will get a score of 5)

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 Scribe · Aug 29, 2015 at 01:18 PM 0
Share

Add your current code, and what you have already tried please.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by legion_44 · Aug 29, 2015 at 02:47 PM

Assuming you already have the script to show the UI, and you're using C# and uGUI (the new gui). Also you will want to handle the case where time variable will be 0.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class TimerScore : MonoBehaviour
 {
     public Text text;
     public int startTime; // we will count down from this value
 
     private int time;
 
     public void StartTimer()
     {
         time = startTime;
         StartCoroutine("timer");
     }
 
     public void StopTimer()
     {
         StopCoroutine("timer");
         text.text = "Score: " + time;
     }
 
     private IEnumerator timer()
     {
         while(time > 0)
         {
             time--;
             yield return new WaitForSeconds(1f);
         }
     }
 }
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 MartSeishuuin_29 · Aug 31, 2015 at 02:40 PM 0
Share

Thanks for the help! After hours and hours of trial and error to convert this to javascript and I was able to create one. What im missing is the StopTime() function to get time remaining or current time when I press a button.

Here's the script that I made.

  var toggleGUI : boolean;
  var timer : float = 10.0f;
  var getTime : int = 0;
  var time : boolean;
      
      function Update ()
      {
          if (time == true)
          {
          timer -= Time.deltaTime;
          }
      }
      
      function OnTriggerEnter (other : Collider)
      {
          time = true;
          toggleGUI = true;
      }
      
      function OnGUI () {
      if (toggleGUI == true){
      GUI.Box (Rect (0, 0, Screen.width, Screen.height), "Question #1");
      GUI.Label (Rect(Screen.width/2-100/2, 50, 150, 20), "Ano mas malaki?");
      GUI.Label (Rect(Screen.width/2-100/2, 25, 150, 20), "Remaining Time: " + timer.ToString("0"));
      
       if (GUI.Button (Rect (Screen.width/2-100/2,130,120,20), "A. Correct Answer"))
           {
               correct();
              }
          if (GUI.Button (Rect (Screen.width/2-100/2,160,120,20), "B. Wrong Answer"))
              {
                  //game over
              }
          if (GUI.Button (Rect (Screen.width/2-100/2,190,120,20), "C. Wrong Answer"))
              {
                  //game over
              }
          
          if (GUI.Button (Rect (Screen.width/2-100/2,220,120,20), "D. Wrong Answer"))
              {
              //game over
          }
     }
 }
 
     function correct()
     {
         TimerStop();
         Game$$anonymous$$aster.currentScore += getTime * 100;
          Destroy(gameObject);
     }
     
     
     
     function TimerStop()
     {
         getTime = timer - Time.deltaTime;
     }

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I switch scenes with countdown timer and Score at the same time 0 Answers

How do I stop a scene from switching from switch before the Countdown timer reaches zero 2 Answers

How do add scenes on the Countdown Timer 0 Answers

How would add an scene on the Countdown Timer 1 Answer

How do I call an if and else statement on a Countdown timer Script 0 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