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;
}
}
Your answer
Follow this Question
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