- Home /
How to save time left in my victory scene
Hi guys, I already make time countdown but I want to save how many time left when player has won the game. Please help me fix the coding.
private float startingTime; public float totalTime; public Text timeLeft;
public Text text;
private float minutes;
private float seconds;
public GameObject GameOver;
[Header("--Event Timer--")]
public bool useEventTimer;
public UnityEvent TimerEvent;
void Start()
{
startingTime = totalTime;
Time.timeScale = 0;
timeLeft.text = PlayerPrefs.GetFloat("Time Left", 0).ToString();
}
void Update()
{
totalTime -= Time.deltaTime;
minutes = (int)(totalTime / 60);
seconds = (int)(totalTime % 60);
if (useEventTimer)
{
if(minutes <= 0 && seconds <= 0)
{
totalTime = startingTime;
Debug.Log("Timer Event");
TimerEvent.Invoke();
GameOver.SetActive(true);
Time.timeScale = 0f;
//GameIsPaused = true;
}
}
text.text = minutes.ToString() + ":" + seconds.ToString();
float timeUsed = totalTime - startingTime;
if (timeUsed > PlayerPrefs.GetFloat("Time Left", 0))
{
PlayerPrefs.SetFloat("Time Left", timeUsed);
timeLeft.text = timeUsed.ToString();
}
}
}
why are you setting time .timescale to 0? it is not going to run since that just pauses every frame dependant like time.deltatime
The time will run after I click something and then whenever player win I want to stop the time and record time left
isnt eexactly what you did here ?
if (timeUsed > PlayerPrefs.GetFloat("Time Left", 0))
{
PlayerPrefs.SetFloat("Time Left", timeUsed);
timeLeft.text = timeUsed.ToString();
}
how are we suppost to help you without you telling us whats happening and why its not working? you need to share some context or it will get rejected, and pplease dont share your comments as answers
Your answer
Follow this Question
Related Questions
How to stop a video using the OCULUS script MoviePlayerSample.CS. 0 Answers
Moving Camera/Player in VR using OVR Raycaster? 0 Answers
how to implement canvas in google cardboard? 3 Answers
Oculus OVR 0 Answers
unity vr app build for android, stuck at one point 0 Answers