Save timer score between scenes? (C#)
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:
public void OnCollisionEnter2D(Collision2D col)
{
if(col.gameObject.tag == "Player")
{
Application.LoadLevel(3);
}
}
What should happen when you say "Load into another scene"? Is there a script attached to some object in that scene that uses the time / score you're sending?
If so, you could make some
public static float currentScore;
public static bool receivedScore;
values and assign them from your OncollisionEnter2D call above.
It's supposed to be sending the score to the other scene, and by "score" i mean send the float number in the timer to the other scene. The timer is being shown visually on the screen with the "Text" variable and I want to save the data on how many seconds the player survived. Then stop the timer and send the float number from the timer to the other scene so it can be displayed.
$$anonymous$$hm, but is there a script on some object in your other scene waiting to receive this value?
Hi!
You can save the score value using the PlayerPrefs. http://docs.unity3d.com/ScriptReference/PlayerPrefs.html
Answer by Internetman · Mar 16, 2016 at 03:53 PM
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")
{
}
}
}
if(Application.loadedLevelName == "Scene 1 $$anonymous$$ain Game")
{
timer += Time.deltaTime;
}
There we go, thank you it works at last! :D I wrote what you said and then created a Start function and wrote:
void Start()
{
if(Application.loadedLevelName == "Scene 1 $$anonymous$$ain Game")
{
timer = 0.0f;
}
}
good job you are beco$$anonymous$$g more professional.
Answer by Verzoto · Mar 16, 2016 at 05:47 PM
Hello, @Internetman!
You can save the score value using the PlayerPrefs
PlayerPrefs.SetFloat("Player Score", 10.0F);
And you can get the score in the other scene using
float score = PlayerPrefs.GetFloat("Player Score");
Answer by Mwarw · Mar 18, 2016 at 10:00 AM
If you save copy of timer as static variable, this should help
static float timerCopy;
function Start(){
timer = timerCopy;
}
function Update(){
timerCopy = timer
}
Answer by Ali-hatem · Mar 16, 2016 at 03:17 PM
add this code to the TimerScript & make the object that this script attached to a brefab after that drag the object into the next scene.
void Awake()
{
DontDestroyOnLoad(this.gameObject);
}
Your answer
Follow this Question
Related Questions
How to get scene name at certain buildIndex 5 Answers
First scene loads twice 1 Answer
Changing player position when changing scene 1 Answer
How to fix Loading Screen? 0 Answers
Application.LoadLevel crashes unity 0 Answers