- Home /
Values not moving to Game Over screen or the Game over doesn't somehow work.
So, in my game I have a timer that runs, once the player dies I would like to show in the game over screen his current, last and best time. Right now the values aren't passed at all to the game over screen, it doesn't even give me a oo:oo:oo.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class Timer : MonoBehaviour {
public Text timerText;
public Text currentText;
public Text previousText;
public Text bestText;
private float milliseconds = 0;
private float seconds = 0;
private float minutes = 0;
private float preMilliseconds = 0;
private float preSeconds = 0;
private float preMinutes = 0;
private float bestMilliseconds = 0;
private float bestSeconds = 0;
private float bestMinutes = 0;
private DeathController death;
// Use this for initialization
public void Awake()
{
if (File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.OpenRead(Application.persistentDataPath + "/playerInfo.dat");
PlayerData data = (PlayerData)bf.Deserialize(file);
file.Close();
preMinutes = data.preMinutes;
preSeconds = data.preSeconds;
preMilliseconds = data.preMilliseconds;
bestMinutes = data.bestMinutes;
bestSeconds = data.bestSeconds;
bestMilliseconds = data.bestMilliseconds;
}
}
private void Start() {
timerText = GetComponent<Text>() as Text;
currentText = GetComponent<Text>() as Text;
previousText = GetComponent<Text>() as Text;
bestText = GetComponent<Text>() as Text;
}
// Update is called once per frame
private void Update () {
if (!GameObject.Find("Death Controller").GetComponent<DeathController>().gameOver)
{
minutes = (int)(Time.timeSinceLevelLoad / 60f);
seconds = (int)(Time.timeSinceLevelLoad % 60f) % 60;
milliseconds = (int)(Time.timeSinceLevelLoad * 1000f) % 1000;
timerText.text = "Time Survived:" + minutes.ToString("00") + ":" + seconds.ToString("00") + ":" + milliseconds.ToString("00");
}
if (GameObject.Find("Death Controller").GetComponent <DeathController>().gameOver) //here's the code for doing the thing that doesn't work
{
if (milliseconds >= bestMilliseconds && seconds >= bestSeconds && minutes >= bestMinutes)
{
bestMilliseconds = milliseconds;
bestSeconds = seconds;
bestMinutes = minutes;
}
preMilliseconds = milliseconds;
preSeconds = seconds;
preMinutes = minutes;
currentText.text = "Time Survived:" + minutes.ToString("00") + ":" + seconds.ToString("00") + ":" + milliseconds.ToString("00");
previousText.text = "Previous Time:" + preMinutes.ToString("00") + ":" + preSeconds.ToString("00") + ":" + preMilliseconds.ToString("00");
bestText.text = "Best Time:" + bestMinutes.ToString("00") + ":" + bestSeconds.ToString("00") + ":" + bestMilliseconds.ToString("00");
Save();
}
}
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file;
if (File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
{
file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
}
else file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
PlayerData data = new PlayerData();
data.preMinutes = preMinutes;
data.preSeconds = preSeconds;
data.preMilliseconds = preMilliseconds;
data.bestMinutes = bestMinutes;
data.bestSeconds = bestSeconds;
data.bestMilliseconds = bestMilliseconds;
bf.Serialize(file, data);
file.Close();
}
[Serializable]
class PlayerData
{
public float preMilliseconds;
public float preSeconds;
public float preMinutes;
public float bestMilliseconds;
public float bestSeconds;
public float bestMinutes;
}
}
![alt text][1] I was also thinking maybe the way I've connected the file's in unity might be a cause, so here's a picture of that too. Ideas what's wrong? Edit: Started wondering that maybe the fault could be that the Game Over doesn't actuaööy work, and it's not about the values moving? [1]: /storage/temp/116000-nimeton.png
nimeton.png
(70.7 kB)
Comment
Your answer
Follow this Question
Related Questions
Passing More Then One Int Into Another Script As A GUI 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
My script only partially works 2 Answers