Hello my fellow Unities, I have a timer in my game that’s counting upwards, the timer is also used for score so 30 seconds = 30 points. Now I’m trying to figure out how to save the timer score, when switching scenes. Say that you survive for 40 seconds, now I want to save that float number and load it into the other scene? Note that I change scene when an enemy collides with the player.
I have my timer code here:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class TimerScript : MonoBehaviour
{
//Create timer
public float timer = 0f;
//Create displayed timer
Text text;
void Awake()
{
//Load Text class
text = GetComponent<Text> ();
//Reset the timer
timer = 0f;
}
void Update()
{
//Start the timer and keep it going
timer += Time.deltaTime;
//Write out the timer on the screen with one decimal
text.text = "" + timer.ToString("F1");
}
}
And here’s the change scene script when an enemy “kills” the player:
Alright, so I kind of got it to work by using some good ol’ IF statements, now the only problem is how do i stop the timer from counting when I’m in the next scene, and then reset it when going back again? (I have a reset button in the scene I’m loading in) If i write “Timer = 0” in the Awake fuction, it will be 0 in both scenes…
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class TimerScript : MonoBehaviour
{
//Create timer
public static float timer = 0f;
//Bool
bool stopTimer = false;
//Create displayed timer
Text text;
void Awake()
{
//Load Text class
text = GetComponent<Text> ();
DontDestroyOnLoad (this.gameObject);
//Reset the timer
//timer = 0f;
}
void Update()
{
//Start the timer and keep it going
timer += Time.deltaTime;
//Write out the timer on the screen
text.text = "" + timer.ToString("F1");
if(Application.loadedLevelName == "Scene 2 Game over")
{
text.text = timer.ToString("F1") + " Points";
}
if(Application.loadedLevelName == "Scene 1 Main Game")
{
}
}
}