So I've got two classes . First class is a class in which there is a timer .
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class helpmetimer : MonoBehaviour
{
public Text timerText;
private float startTime;
private bool finnished = false;
void Start()
{
startTime = Time.time;
}
// Update is called once per frame
void Update()
{
if (finnished)
return;
float t = Time.time - startTime;
string minutes = ((int)t / 60).ToString();
string seconds = (t % 60).ToString("f0");
timerText.text = minutes + ":" + seconds;
}
public void Finnish()
{
finnished = true;
timerText.color = Color.yellow;
}
}
the second is class is the one which I use to save data i.e name, score and timerText using JSON. I have no problems saving name which is a string and score which is an integer . Problem comes when I try to save the timerText.
Second class which I've used for initialization is ..
using System;
using System.Collections.Generic;
using UnityEngine;
[Serializable]
public class helperclass
{
public string name;
public int score;
//dunno what is to be used for timer.
}
and finally the 3rd class that is used to save data.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class savegameformaterial1 : MonoBehaviour
{
static readonly string SAVE_FILE = "new.json";
void Start()
{
helperclass data = new helperclass() { name = "ravi", score =0 };
string json = JsonUtility.ToJson(data);
Debug.Log(json);
string filename = Path.Combine(Application.persistentDataPath, SAVE_FILE);
if (File.Exists(filename))
{
File.Delete(filename);
}
File.WriteAllText(filename, json);
Debug.Log("Player saved to" + filename);
}
void Update()
{
helperclass data = new helperclass() { name = "billiejoe", score = score4.scoreValue };
string json = JsonUtility.ToJson(data);
Debug.Log(json);
string filename = Path.Combine(Application.persistentDataPath, SAVE_FILE);
if (File.Exists(filename))
{
File.Delete(filename);
}
File.WriteAllText(filename, json);
Debug.Log("Player saved to" + filename);
}
}
If I can save timerText or time in the same manner I am able to save name and score then my task would be completed. Any help will be appreciated . I tried to convert timerText to string and then access it but its not working . Then I tried converting it to a char array but still no luck . This is a part of the game in which I have to pick a certain object and place it at a certain position . if there is a failed attempt that is stored in the variable text . Name is written beforehand and the time taken for the process is named timertext .