How do I use PlayerPrefs properly?
Hi, I'm trying to use my code as a countdown and race timer, which all works completely fine. But I am unsure of how to use PlayerPrefs.SetString properly so that I can output my race time to another scene which displays statistics. After setting the string I will make another script which will get the string and display it on a GUI Text.
This is what I have so far:
using UnityEngine;
using System.Collections;
public class CountdownTimer : MonoBehaviour
{
// declaration
private GameObject ball;
private GameObject ballPosition;
public float timeRemaining = 4;
private GUIText myText;
public GUIText myTimer;
private float startTime;
private bool finished = false;
void Start()
{
// assigns the game object "Ball" to private variable 'ball'
ball = GameObject.Find("Ball");
// assigns the game object "Ball Position" to private variable 'ballPosition'
ballPosition = GameObject.Find("Ball Position");
// assigns the GUI Text component to private variable 'myText' and 'myText2'
myText = GetComponent<GUIText>();
}
void Update ()
{
// if the time remaining is greater than zero
if (timeRemaining > 0.0f)
{
// initialises a countdown from timeRemaining float value, 4
timeRemaining -= Time.deltaTime;
// converts float to int and displays countdown on screen
myText.text = ((int)timeRemaining).ToString();
}
if (timeRemaining > 1.0f)
{
startTime = Time.time;
}
// if the time remaining is less than one
if(timeRemaining < 1.0f)
{
// displays text on screen after the countdown finishes
myText.text = "GO!";
}
// if the time remaining is less than zero
if(timeRemaining < 0)
{
// disables the GUI Text component
myText.enabled = false;
}
// if the time remaining is less than one
if(timeRemaining < 1.0f)
{
// enables the Play Maker FSM component for user input
ball.GetComponent<PlayMakerFSM>().enabled = true;
ballPosition.GetComponent<PlayMakerFSM>().enabled = true;
}
if(finished)
return;
float t = Time.time - startTime;
string minutes = ((int)t / 60).ToString();
string seconds = (t % 60).ToString("f2");
myTimer.text = minutes + ":" + seconds;
}
void OnTriggerEnter(Collider other)
{
ball.SendMessage ("Finish!");
ball.GetComponent<PlayMakerFSM>().enabled = false;
}
public void Finish()
{
finished = true;
if (finished = true)
{
PlayerPrefs.SetInt("Race Timer", myTimer);
}
}
}
Thanks in advance!
Answer by TBruce · May 12, 2016 at 06:37 PM
The following saves the myTimer.text strng to PlayerPrefs in the Finish() function. I also added the functionality to load the statistics scene if you need it.
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class CountdownTimer : MonoBehaviour
{
public GUIText myTimer;
public string statisticsScene;
public void Finish()
{
finished = true;
if (finished = true) // why this conditional
{
PlayerPrefs.SetString("Race Timer", myTimer.text);
if (statisticsScene != "") // the string statisticsScene must be set in the inspector or a constant value
{
SceneManager.LoadScene(statisticsScene);
}
}
}
}
Next, the following Statistics script will set myTimer.text with the value from "Race Timer" in PlayerPrefs if it exists. If it does not it will display the default value of "00:00".
using UnityEngine;
using System.Collections;
public class Statistics : MonoBehaviour
{
public GUIText myTimer;
void Start()
{
if (myTimer != null)
{
myTimer.text = PlayerPrefs.GetString("Race Timer", "00:00");
}
}
}
@$$anonymous$$avina This is great! Exactly what I needed! Thank you for your time and help, it will be very useful to me in the future. :D
NP. I forgot to mention above that first
if (finished = true)
is incorrect. If you are going to do a statement liker that it needs to be
if (finished == true)
with two equal signs. But as I mention why are you doing this when you set finished to true right above?
In short, its because of my scene setup, I'm using Play$$anonymous$$aker aswell.
Your answer
Follow this Question
Related Questions
Faster Way to Increment PlayerPrefs Int 1 Answer
Playerprefs, i dont know how to use 0 Answers
Editor PlayerPrefs getting repopulated with deleted values 2 Answers
standard anti memory hack load save data using playerprefs 0 Answers
Persistent data between different games - local save for WebGL 0 Answers