0

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 .

Furkan Kambay
  • 751
  • 1
  • 7
  • 18
ravi tanwar
  • 598
  • 5
  • 16
  • Can you state how you tried this? "tried to convert timerText to string and then access it but its not working ".Because timerText.text is a string and I think it can be saved . – Sourav Sachdeva Apr 05 '18 at 05:31
  • timerText.text = minutes + ":" + seconds;//this is the final line in timer class i added timerText.ToString in the end . Is this how we convert text to string ? – ravi tanwar Apr 05 '18 at 05:35
  • 1.Do not save file *every frame*. That's really awful. Do that in the `OnApplicationPause` function once the program is about to exit. 2.The duplicate has a function to serialize and save data at once. If you don't want to use the wrapper, the code in it should get you started on how to make your own. – Programmer Apr 05 '18 at 06:00
  • You are supposed to load the saved data in the `Start` or `Awake` function then save it in the `OnApplicationPause` function – Programmer Apr 05 '18 at 06:01

1 Answers1

0
      public class helperclass
        {
            public string name;
            public int score;
            public string time; //dunno what is to be used for timer.
        }
        void Start()
            {
 GameObject timerobject= GameObject.Find("timerobject");
            helpmetimer  htimer= timerobject.GetComponent<helpmetimer>();

                helperclass data = new helperclass() { name = "ravi", score =0 ,time=htimer.timerText.text };

            ...
}

timerobject is the gameobject on which the helpmetimer is attached.

Hope this helps.

Sourav Sachdeva
  • 353
  • 6
  • 20