Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Qxintero · May 12, 2016 at 01:53 PM · playerprefs

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by TBruce · May 12, 2016 at 06:37 PM

@Qxintero

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");
         }
     }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Qxintero · May 13, 2016 at 02:21 AM 0
Share

@$$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

avatar image TBruce Qxintero · May 13, 2016 at 02:26 AM 0
Share

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?

avatar image Qxintero TBruce · May 13, 2016 at 03:21 PM 0
Share

In short, its because of my scene setup, I'm using Play$$anonymous$$aker aswell.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

57 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges