Save timer score from different scenes
Im trying to build a game were the player should try and localize sound sources and then time them and save it. Then move on to the next scene and do the same thing but with a different sound. Right know I have a trigger on a gameobject which send the player to the next scene with :
Application.LoadLevel("level2");
But I cant seem to get a timer to work in the way that I want to. Can I get some help on what I should look for?
Answer by dhore · Dec 13, 2015 at 12:53 AM
So what you're trying to do is time the player, but keep adding on to that time across the different scenes rather than start again from 0, correct?
If so, you can just use a Static Variable which will stay the same across all scenes. See this question for an example.
EDIT: @Ajizi after your answer containing more info (not sure why you posted that as an answer though?) I now know exactly what you are looking for - just simply a way to make a timer. Well you're on the right track with Time.time; here's the code you need:
// at start of game/round
float startTime = Time.time;
// when player finishes game/round
float totalTime = Time.time - startTime;
As for saving to a text file, there's a nice explanation here.
Thanks man, time.time seems to be working out fine for my purpose. I ended up doing something like this:
using UnityEngine;
using System.Collections;
using System.IO;
public class Trigger : $$anonymous$$onoBehaviour {
private float timer;
void OnTriggerEnter(Collider sceneSwitch)
{
timer = Time.timeSinceLevelLoad;
Debug.Log(timer);
timer.ToString();
StreamWriter dataTimer = new StreamWriter(@"C:\data.txt", true);
dataTimer.WriteLine(timer);
dataTimer.Close();
Application.LoadLevel(Application.loadedLevel + 1);
}
}
It would be nice if the data.txt file was saved inside the same map as the .exe file though.
Answer by Ajizi · Dec 13, 2015 at 10:24 AM
Im looking into time.time at the moment. I dont really need a visual timer. I need it to start when the game starts and stop it and save it to a file when the player finds the object. And then be able to do it again on a different scene with a different sound.
Your answer
Follow this Question
Related Questions
LoadScene() does not reload my GUI C# 2 Answers
Unity 5.1 networking 0 Answers
Post time at the end of the level 0 Answers
Countdown timer in multiple scene? 1 Answer
Gear VR template scene for Unity 5.1+ ? 0 Answers