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 importguru88 · Apr 22, 2016 at 04:19 PM · c#timetimerscorescore system

How do I add my Timer to my Score system in C# ?

Basically I have set time for 2:00 mins and score is set at 50 points . If the timer is at 0 and the player has the points the scene changes something like that . I think am missing some information Here is some of my code :

using UnityEngine; using UnityEngine.UI; using UnityEngine.Events; using System; using System.Collections;

[System.Serializable] public class DisplayOptions{ public bool milliseconds, seconds, minutes, hours, days; } [System.Serializable] public class TimerFormat{ public bool milliseconds, seconds, minutes; }

[ExecuteInEditMode] public class STK_Timer : MonoBehaviour { public enum TimerType{CountUp, CountDown, CountUpInfinite} public enum LoadSceneOn{Disabled, TimesUp, SpecificTime} public enum TimerState{Disabled, Counting} public DisplayOptions displayOptions; public TimerFormat timerFormat; public static int Points; Text text;

 private string ms, s, m, d, h;

 [Header("Timer Settings")]
 public TimerState timerState;
 public bool useSystemTime;
 private DateTime systemDateTime;
 private float gameTime;
 public Text timerText;
 public string timePrefix;
 public float timerSpeed;
 public float timeScale;
 public int day;
 public int hour;
 public int minute;
 public int second;
 public int millisecond;

 [Header("Times Up Settings")]
 public bool setZeroTimescale;
 public UnityEvent timesUpEvent;
 public GameObject[] destroyOnFinish;
 [HideInInspector()] public TimerType timerType;
 [HideInInspector()] public float startTime;
 [HideInInspector()] public float finishTime;
 [HideInInspector()] public LoadSceneOn loadSceneOn;
 [HideInInspector()] public string loadSceneName;
 [HideInInspector()] public float timeToLoadScene;
 
 string FormatSeconds(float elapsed){
     if (timerType == TimerType.CountUpInfinite) {
         if (useSystemTime) {
             CheckDisplayOptions ();
             gameTime = ((float)DateTime.Now.Hour + ((float)DateTime.Now.Minute) + (float)DateTime.Now.Second);
             millisecond = (int)DateTime.Now.Millisecond;
             second = (int)DateTime.Now.Second;
             minute = (int)DateTime.Now.Minute;
             hour = (int)DateTime.Now.Hour;
             day = (int)DateTime.Now.DayOfYear;
             return String.Format (d + h + m + s + ms, day, hour, minute, second, millisecond);
         } else {
             CheckDisplayOptions ();
             TimeSpan t = TimeSpan.FromSeconds (elapsed);
             day = t.Days;
             hour = t.Hours;
             minute = t.Minutes;
             second = t.Seconds;
             millisecond = t.Milliseconds;

             return String.Format ( d + h + m + s + ms, 
                 t.Days, t.Hours, t.Minutes, t.Seconds, t.Milliseconds);
         }


     } else {            
         int d = (int)(elapsed * 100.0f);
         int minutes = d / (60 * 100);
         int seconds = (d % (60 * 100)) / 100;
         int hundredths = d % 100;
         CheckTimerOptions ();
         return String.Format ( m + s + ms , minutes, seconds, hundredths);
     }
 }

 void CheckDisplayOptions(){
     if (displayOptions.milliseconds) {
         ms = ".{4:D3}";
     } else {
         ms = "";
     }
     if (displayOptions.seconds) {
         s = "{3:D2}";
     } else {
         s = "";
     }
     if (displayOptions.minutes) {
         m = "{2:D2}:";
     } else {
         m = "";
     }
     if (displayOptions.hours) {
         h = "{1:D2}:";
     } else {
         h = "";
     }
     if (displayOptions.days) {
         d = "{0:D3}:";
     } else {
         d = "";
     }
 }

 void CheckTimerOptions(){
     if (timerFormat.milliseconds) {
         ms = ".{2:00}";
     } else {
         ms = "";
     }
     if (timerFormat.seconds) {
         s = "{1:00}";
     } else {
         s = "";
     }
     if (timerFormat.minutes) {
         m = "{0:00}:";
     } else {
         m = "";
     }
 }
 
 void Start () {
 text = GetComponent <Text>();
          Points = 0;
     Time.timeScale = timeScale;
     if(timerState == TimerState.Counting){
         timerText.text = timePrefix + FormatSeconds(gameTime);
         if (timerType == TimerType.CountUp) {
             gameTime = 0;
         }
         if (timerType == TimerType.CountDown) {
             gameTime = startTime;
         }
     }
 }

 void Update () {
     Time.timeScale = timeScale;
     #if UNITY_EDITOR
     if(!Application.isPlaying){
         if (timerType == TimerType.CountUp) {
             gameTime = 0;
             timerText.text = timePrefix + FormatSeconds(gameTime);
         }
         if (timerType == TimerType.CountDown) {
             gameTime = startTime;
             timerText.text = timePrefix + FormatSeconds(gameTime);
         }
         if(timerType == TimerType.CountUpInfinite)
             timerText.text = timePrefix + FormatSeconds(gameTime);
     }
     #endif
     if (timerState == TimerState.Counting) {
         if (timerType == TimerType.CountUp) {
             gameTime += 1 * Time.deltaTime * timerSpeed;
             if (gameTime >= timeToLoadScene) {
                 if (loadSceneOn == LoadSceneOn.SpecificTime) {
                     Application.LoadLevel (loadSceneName);
                 }
             }
         }
         if (timerType == TimerType.CountDown) {
             gameTime -= 1 * Time.deltaTime * timerSpeed;
             if (gameTime <= timeToLoadScene) {
                 if (loadSceneOn == LoadSceneOn.SpecificTime) {
                     Application.LoadLevel (loadSceneName);
                 }
             }
         }
         if (timerType == TimerType.CountUpInfinite) {
             if (useSystemTime) {
                 
             } else {
                 gameTime += 1 * Time.deltaTime * timerSpeed;
             }
         }
         if (timerType == TimerType.CountDown && gameTime <= 0) {
             StopTimer ();
             timesUpEvent.Invoke();
             if(setZeroTimescale)
                 Time.timeScale = 0;
             for (int i = 0; i < destroyOnFinish.Length; i++) {
                 Destroy (destroyOnFinish [i]);
             }
         }
         if (timerType == TimerType.CountUp && gameTime >= finishTime) {
             timesUpEvent.Invoke();
             StopTimer ();
             if(setZeroTimescale)
                 Time.timeScale = 0;
             for (int i = 0; i < destroyOnFinish.Length; i++) {
                 Destroy (destroyOnFinish [i]);
             }
             if (loadSceneOn == LoadSceneOn.TimesUp)
                 Application.LoadLevel(loadSceneName);
         }
         timerText.text = timePrefix + FormatSeconds(gameTime);

     }
 }

 [ContextMenu("Start Timer")]
 public void StartTimer(){
     timerState = TimerState.Counting;
 }

 [ContextMenu("Stop Timer")]
 public void StopTimer(){
     timerState = TimerState.Disabled;
 }

 [ContextMenu("Restart Timer")]
 public void RestartTimer(){
     if (timerType == TimerType.CountDown) {
         gameTime = startTime;
     } else {
         gameTime = 0;
     }
     timerText.text = timePrefix + FormatSeconds(gameTime);
     StartTimer ();
 }

 [ContextMenu("Reset Timer")]
 public void ResetTimer(){
     if (timerType == TimerType.CountDown) {
         gameTime = startTime;
     } else {
         gameTime = 0;
     }
     timerText.text = timePrefix + FormatSeconds(gameTime);
 }

 public float GetTimerValue(){
     return gameTime;
 }


 void  Score()
 {
     text.text = "Points:" + Points;
     
 }

}

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

0 Replies

· Add your reply
  • Sort: 

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

Looking for a simple Score Multiplier. 1 Answer

Scoring system not working. 1 Answer

Score counted multiple times when several projectiles impact the same target 1 Answer

Clock doesn't stop when reaches 0.0. Can you help me with this. 1 Answer

How to create a timer that only increases when if command is true? 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