- Home /
Using Playerprefs???
Ok. I know i'm meant to use playerprefs but i keep getting errors i want to save my high score so it displays on the main menu when you return to it and every tome you load the game up how would i do this?
The attached is what ive got that gets the score every time i go through the checkpoint any help would be really appreciated :) Thanks.
Please do not post code as an image, post code as simple code and use the 10101 button to highlight it.
Sorry about that never posted on here before so here's the code again :)
var prefabExplode: Transform;
var prefabGameOver: Transform;
function OnTriggerEnter( collision : Collider )
{
if(collision.gameObject.tag == "walls")
{
var prefabExplode = Instantiate(prefabExplode, gameObject.transform.position, transform.rotation);
var prefabGameOver = Instantiate(prefabGameOver, gameObject.transform.position, transform.rotation);
Destroy(gameObject);
}
if(PlayerPrefs.Has$$anonymous$$ey("score")){
myScore = PlayerPrefs.GetInt("score");
}else{
myScore = 0;
}
if(PlayerPrefs.Has$$anonymous$$ey("score")){
PlayerPrefs.SetInt("score", $$anonymous$$athf.$$anonymous$$ax(myScore, PlayerPrefs.GetInt("score")));
}else{
PlayerPrefs.SetInt("score", myScore);
}
{
CheckPoint.Score +=1;
z_axis_forward.ZAxisSpeed +=0.003;
}
}
Answer by wijesijp · Mar 25, 2014 at 09:00 AM
Your problem maybe line 15 Destroy(gameObject)
If you want to destroy other object you have to use Destroy(collision.gameObject)
Answer by JuanGomez · Mar 25, 2014 at 09:10 AM
This is when reading:
if(PlayerPrefs.HasKey("score")){
myScore = PlayerPrefs.GetInt("score");
}else{
myScore = 0;
}
This is for writing:
if(PlayerPrefs.HasKey("score")){
PlayerPrefs.SetInt("score", Mathf.Max(myScore, PlayerPrefs.GetInt("score"));
}else{
PlayerPrefs.SetInt("score", myScore);
}
Good luck :)
Thanks for that response i had to tweak it just a little so i didn't get any errors :)
but i'm still not sure if i'm doing this right or not, do i need to add this script to a game object or something? so that i can get the high score to save on the main menu as at the moment it doesn't seem to be doing anything yet?
I use to use game maker 8 so this is a big step for me...lol
Thanks again.
Answer by Fappp · Mar 25, 2014 at 09:22 AM
Try changing this line:
if(collision.gameObject.tag == "walls";
With this one:
if (collision.CompareTag ("walls")) {
If you get an error add gameObject to it:
if (collision.gameObject.CompareTag ("walls")) {
Please let met know if this fixed your issue!
Answer by corriedotdev · Mar 25, 2014 at 09:26 AM
You have on trigger enter? You not want OnCollisionEnter? Especially if your object is tagged.
Your answer
Follow this Question
Related Questions
UnitySerializer Only Saving one Game Problem 0 Answers
Save Transform in PlayerPrefs 2 Answers
Mass Saving/Loading 0 Answers
Game Inside Game 0 Answers
How do you enable saving on android? 1 Answer