- Home /
Storing in PlayerPref based on timer
Officially first post on here so take it easy on me. I'm using a js to countdown (using 2 minutes). What I need is to store a 3 (for 3 stars) if they get it done in 1:40-1:50. Store a 2 (for 2 stars) if they get done in 1:50 to 2:00. And store a 1 (for 1 star) if they finish after 2:00. I would then use this value after the next level to display how many stars. I would also use this to unlock a secret level if 3 stars were acheived on all 5 levels. Code for Timer:
var startTime: float; var timeRemaining:float; function Start(){ startTime = 120.0; guiText.material.color = Color.red; } function Update () { Countdown(); } function Countdown(){ timeRemaining = startTime - Time.time; ShowTime(); if(timeRemaining < 0) { timeRemaining = 0; TimeIsUp();
Debug.Log("Time remaining = "+ timeRemaining);
}
} function ShowTime(){ var minutes: int; var seconds: int; var timeString : String; minutes = timeRemaining/60; seconds = timeRemaining%60; timeString = minutes.ToString() +":" + seconds.ToString("D2"); guiText.text = timeString; } function TimeIsUp(){ Debug.Log("Time is up!"); }
Right now this works and displays "Time is up!" but I'm wanting it to store those values instead. I'm assuming using PlayerPrefs would be the best way. I also would like a way for my box collider to store a true value in PlayerPrefs so that I could use it for my level unlock screen. Any help is appriciated and I can post more code or info about my game if needed. Thanks
Great first ? but you didn't format your code =P No biggie though, great job, again! =)
Answer by Justin Warner · Apr 13, 2011 at 01:59 AM
Ok, by not having what the game is, I'll go with what I can.
On the condition that the player HAS finished the game/level, run a function (Done), and pass through the timeRemaining.
Ok, by not having what the game is, I'll go with what I can.
On the condition that the player HAS finished the game/level, run a function (Done), and pass through the timeRemaining.
function Done(float time){
if(time <= 110.0)
{
PlayerPrefs.SetFloat("Player Stars", PlayerPrefs.GetFloat("Player Stars")+3);
}
else if(time <= 120&& time >= 110)
{
PlayerPrefs.SetFloat("Player Stars", PlayerPrefs.GetFloat("Player Stars")+2);
}
else if(time > 120)
{
PlayerPrefs.SetFloat("Player Stars", PlayerPrefs.GetFloat("Player Stars")+1);
}
}
If you have any questions, ask... But this should make sense.... Hope it helped!
Ok everything makes sense but I have never used PlayerPrefs. So basically this calls the Player Stars and adds either 1, 2, or 3. Then when I access it in my completed level or complete game scenes I use PlayerPrefs.GetFloat("Player Stars"). I'm assu$$anonymous$$g player prefs can be accessed in any scene..? Also any links to using PlayerPrefs to unlock levels? And thanks that helps a ton!
For the unlock levels, you just do if(PlayerPrefs.GetFloat("Player Stars" ) >= 10) { //GUI STUFF ] It's really easy... And yes, you have that right =)
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
PlayerPrefs Question 0 Answers
Seamless Room Transitions 0 Answers
How do you save timer with PlayerPrefs? 1 Answer
Help With Quickest TimeScore 0 Answers