how to reset my timer each time after death?

Updated on October 31, 2017 in [A] 2D
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
2 on October 30, 2017

hey my timer works perfectly each time i run my game but once the character dies it doesnt reset the time back to 0 i need help with this please help me!

 

GameManager(script);

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour {
private Timer thescoremanager;
public float slowness = 10f;

private void Start()
{
thescoremanager = FindObjectOfType<Timer>();
}

public void EndGame ()
{
StartCoroutine(RestartLevel());
}

IEnumerator RestartLevel ()
{
thescoremanager = FindObjectOfType<Timer>();
Time.timeScale = 1f / slowness;
Time.fixedDeltaTime = Time.fixedDeltaTime / slowness;

thescoremanager.scoreincreasing = false;
yield return new WaitForSeconds(1f / slowness);

Time.timeScale = 1f;
Time.fixedDeltaTime = Time.fixedDeltaTime * slowness;

SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
thescoremanager.t = 0;
thescoremanager.scoreminute = 0;
thescoremanager.scoresecond = 0;
thescoremanager.scoreincreasing = true;

}

}

 

Timer(Script);

using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;

public class Timer : MonoBehaviour {
public Text timertext;
public Text scoretext;

public float t;
public float scoreminute;
public float scoresecond;
private float highscoreminute;
private float highscoresecond;
private float starttime;
public bool scoreincreasing;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

if (scoreincreasing)
{

t = Time.time;
scoreminute = (int)t / 60;
scoresecond = t % 60;
}

if(scoreminute > highscoreminute && scoresecond > highscoresecond)
{
highscoreminute = scoreminute;
highscoresecond = scoreminute;
}

timertext.text = Mathf.Round(scoreminute) + “:” + Mathf.Round(scoresecond);
scoretext.text = Mathf.Round(highscoreminute) + “:” + Mathf.Round(scoresecond);

}
}

 

 

 

  • Liked by
  • abcodes
Reply
1 on October 31, 2017

I think that is just a sequence error.

Just reset your variables before you load the scene:

 thescoremanager.t = 0;
 thescoremanager.scoreminute = 0;
 thescoremanager.scoresecond = 0;
 thescoremanager.scoreincreasing = true;
 
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);

 

on October 31, 2017

thanks i fixed this like 5 hours after i posted it and forgot to update the post but i cant get this type of movement to work if u can help me it would be grate! 🙂

 

Hey i was wondering how i could change my players movement to a mobile type of movement

Show more replies
  • Liked by
Reply
Cancel