using playerprefs to keep track of time spend in game

so I have made quite a short singleplayer game however I want the player to be able to see how long they took to complete it. I made a timer and placed it off the screen with a don’t destroy script however I want to be able to recall this timer at the end of the game but cannot work out how to.

here is what I have so far

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class TotalTime : MonoBehaviour
{
    public static Text timerText;
    public float hourcount;
    public float minuteCount;
    public float secondsCount;
    public float millisecondsCount;

    public void Savetime()
    {
        PlayerPrefs.SetString("Time", timerText);
        PlayerPrefs.Save();
        print("Time saved!");
    }
    public void Loadtime()
    {
        PlayerPrefs.GetString("Time");
        timerText.text = string.Format("TIME - {0:00}:{1:00}.{2:00}", minuteCount, secondsCount, (int)millisecondsCount);

    }
}

using this code i get an error saying text cannot be converted to a string. if anyone can help it would be much appreciated

Why is timerText static?

because i am referring to it in my timer script. Im still new to coding so not sure if it should be static or not

But where are you setting the timerText reference? Last I checked they aren’t available in the inspector to set there, and you aren’t setting it in your script.

ah. i thought my code would find the text inside the text reference i set and save it.

public void Loadtime()
    {
        PlayerPrefs.GetString("Time"); //This is giving you the string saved to "Time", you're not doing anything with it
        timerText.text = string.Format("TIME - {0:00}:{1:00}.{2:00}", minuteCount, secondsCount, (int)millisecondsCount);
    }

(Check the comment in the code ^)
What are all the count variables you have there?
they aren’t used by anything besides when you set the timer.text, where do they get the value?

The error you’re getting is because when you try to save the string “Time” you’re passing it the value ‘timerText’, this is not a string, this is an instance of a class called Text, what you want is ‘timerText.text’, it gives you the string stored in timerText.

Take note of what @Joe-Censored said, you’re using statics wrong.
you can access a public field of another class, you just need a reference to the instance.

okay i think i understand what you mean, all the count variables are from my other timer script. i thought i needed them but i guess not. So how would i go about doing this and making the time be able to use as text in playerprefs and shown at a later time.

You are calling PlayerPrefs.GetString, but not doing anything with it. Where are you setting minuteCount, etc? I suspect you want to do something like:

string myTime = PlayerPrefs.GetString…

Then parse myTime to get minutes, etc.