Sign up Calendar Latest Topics
 
 
 


Reply
  Author   Comment  
J53 Industries

Senior Member
Registered:
Posts: 107
Reply with quote  #1 
Hello, I have a question on how to save a highscore in unity 3d. So what I have now is two scripts one in the level that creates the score and is supposed to save it. The second script is attached to a game object in the main menu and that script is supposed to display a high score. The first script works like it should adding points however I am not sure if it saves the high score because when the game ends and you go back to the menu the high score is not updated. So I am unsure if something is just not connecting with the two scripts or what. Any help would be greatly appreciated. Thanks in advance.(Disclaimer I am a noob at programming) 

Script 1: 

var scoretext : UI.Text;
private var Score : int = 0;

function Awake() 
{
InvokeRepeating("UpdateScore" , .50, .50);
}
  function UpdateScore() 
  {
  Score += 1;
  scoretext.text = "Score: " + Score.ToString();
}
  
  function GameOver()
{
  if(Score > PlayerPrefs.GetInt("Score")) 
{
  PlayerPrefs.SetInt("Score", Score);
    
    }
    
    Application.LoadLevel("0");
    
    }
    


Script 2: 

var HighScore : UI.Text;
function Awake()
{
  HighScore.text = "HighScore: " + PlayerPrefs.GetInt("Score").ToString();
  

  
}

__________________
Check Out My Apps Below

https://play.google.com/store/apps/developer?id=J53+Industries
0
graceboy

Registered:
Posts: 139
Reply with quote  #2 

  1. PlayerPrefs.SetInt("highScore", highScore);
  2. PlayerPrefs.Save();
  3. currentScore =PlayerPrefs.GetInt("highScore");    

Make sure your saving highscores before exit script

Try if not working reply again
0
J53 Industries

Senior Member
Registered:
Posts: 107
Reply with quote  #3 
Quote:
Originally Posted by graceboy

  1. PlayerPrefs.SetInt("highScore", highScore);
  2. PlayerPrefs.Save();
  3. currentScore =PlayerPrefs.GetInt("highScore");    

Make sure your saving highscores before exit script

Try if not working reply again


Graceboy I greatly appreciate your help. The only thing that I am having trouble with the current score part. Not sure if I should set it as a variable or what. My updated scrit is below and if you could help that would be awesome. Thanks


var scoretext : UI.Text;
private var Score : int = 0;
var currentscore : PlayerPrefs;
function Awake() 
{
InvokeRepeating("UpdateScore" , .50, .50);
}
  function UpdateScore() 
  {
  Score += 1;
  scoretext.text = "Score: " + Score.ToString();
}
  
  function GameOver()
{
  if(Score > PlayerPrefs.GetInt("Score")) 
{
  PlayerPrefs.SetInt("Score", Score);
    
    PlayerPrefs.Save();
   currentScore = PlayerPrefs.GetInt("Score"); 
    
    
    }
    
    Application.LoadLevel("0");
    
    }
    

__________________
Check Out My Apps Below

https://play.google.com/store/apps/developer?id=J53+Industries
0
graceboy

Registered:
Posts: 139
Reply with quote  #4 
Note i programming in c - sharp but i have used javacript 2 years ago so should concentrate whats this code doing .. 

i USE THIS METHOD IN C# . SO first i will use ontriggerenter2d remember you must check on triger in unity editor and tag  coins a tag name  ok then use this code for saving coins 
See->>
var characterInQuicksand: boolean;
var scoreplus = 0;
var savingScoresString : string;
var ScoresStoring = 0 ;
function OnTriggerEnter2D(other: Collider2D) {

if(other.gameObject.tag == "Carrot")
{


Destroy(other.gameObject); --> this will destory coin 
scoreplus++; --> increase count by 1 
PlayerPrefs.SetInt(savingScoresString,ScoresStoring); --> it will set or create if player prefs file not created 
PlayerPrefs.Save(); --> this will save  
Scores.text = scoreplus.ToString() ; --> this will update text 
}

}
Now just get scores where you want 
-->>   PlayerPrefs.GetInt("highScore");    

---> ADDITIONAL RELATED VIDEOS








--> Unityreference will also help you --> http://docs.unity3d.com/ScriptReference/PlayerPrefs.html
i HOPE THIS WILL HELP IF NOT THEN AGAIN REPLY HERE WE WILL FIX ANY ISSUE YOU FACING!
0
J53 Industries

Senior Member
Registered:
Posts: 107
Reply with quote  #5 
Okay I am still stuck. Click on the video link below to see what is happening. This is something that I would really like to figure out for future projects so any help would be greatly appreciated. Thanks in advance


__________________
Check Out My Apps Below

https://play.google.com/store/apps/developer?id=J53+Industries
0
mzr

Senior Member
Registered:
Posts: 222
Reply with quote  #6 
Are you sure that it's an issue with playerprefs?
You can check that out by temporarily using some random value instead of gettint in the last script.
Maybe you forgot to reference the high score text object in the editor?

__________________

http://www.howtomakemobilegames.com/post/twin-stick-shooter-7140396

My second game: Google Play
My first game: Google Play  Amazon iOS

0
J53 Industries

Senior Member
Registered:
Posts: 107
Reply with quote  #7 
Okay I did this and below is a screenshot I put a 3 in for the get int and it appeared. So I am still confused.  Untitled.png 

__________________
Check Out My Apps Below

https://play.google.com/store/apps/developer?id=J53+Industries
0
mzr

Senior Member
Registered:
Posts: 222
Reply with quote  #8 
Ok, then it seems that it's indeed an issue with playerprefs.
I'll take a look when I'm back from work.

__________________

http://www.howtomakemobilegames.com/post/twin-stick-shooter-7140396

My second game: Google Play
My first game: Google Play  Amazon iOS

0
Grumpy

Senior Member
Registered:
Posts: 318
Reply with quote  #9 
When are you refreshing the HighScore.text? If you only call it in that awake then it won't update properly.
0
J53 Industries

Senior Member
Registered:
Posts: 107
Reply with quote  #10 
Quote:
Originally Posted by Grumpy
When are you refreshing the HighScore.text? If you only call it in that awake then it won't update properly.


Okay so what would be a better way to update Highscore.text

__________________
Check Out My Apps Below

https://play.google.com/store/apps/developer?id=J53+Industries
0
mzr

Senior Member
Registered:
Posts: 222
Reply with quote  #11 
Quote:
Originally Posted by Grumpy
When are you refreshing the HighScore.text? If you only call it in that awake then it won't update properly.

Yeah, but it would at least show the data from previous game starts.

__________________

http://www.howtomakemobilegames.com/post/twin-stick-shooter-7140396

My second game: Google Play
My first game: Google Play  Amazon iOS

0
Grumpy

Senior Member
Registered:
Posts: 318
Reply with quote  #12 
Call it whenever you change the highscore.


 function GameOver()
{
  if(Score > PlayerPrefs.GetInt("Score")) 
  {
    PlayerPrefs.SetInt("Score", Score);
    Highscore.text=PlayerPrefs.GetInt("Score");
    Debug.Log(PlayerPrefs.GetInt("Score"));
   }else{
     Debug.Log("Didn't save score");
   }
    
    Application.LoadLevel("0");
}


Try the above, the debug will give you some idea of what's going on.
0
mzr

Senior Member
Registered:
Posts: 222
Reply with quote  #13 
Your code works for me (not sure if it's 100% the same).
I've made a test project, hope it helps.

 
Attached Files
zip J53_Industries.zip (378.89 KB, 5 views)


__________________

http://www.howtomakemobilegames.com/post/twin-stick-shooter-7140396

My second game: Google Play
My first game: Google Play  Amazon iOS

0
J53 Industries

Senior Member
Registered:
Posts: 107
Reply with quote  #14 
Quote:
Originally Posted by mzr
Your code works for me (not sure if it's 100% the same).
I've made a test project, hope it helps.


I loaded this into a new project a still nothing. Sorry about being so persistent I just would really like to know what I am doing wrong. Here is a vid of mzr's scripts in a game.



And as for your script grumpy I get this error 

Assets/AddScore.js(20,33): BCE0022: Cannot convert 'int' to 'UnityEngine.UI.Text'.

Thank you everyone for the help


__________________
Check Out My Apps Below

https://play.google.com/store/apps/developer?id=J53+Industries
0
mzr

Senior Member
Registered:
Posts: 222
Reply with quote  #15 
That's weird.
It looks like there is some issue with your local playerprefs.
Do you have other values saved there? Do they work?
Does highscore work on a device?
You could try deleting playerprefs data, make a new button and add a simple function there with this command PlayerPrefs.DeleteKey("HighScore");
If that doesn't help you can go even further and use PlayerPrefs.DeleteAll(), but this will probably remove all your playerprefs, so don't use it if you have some important data saved there.
If that doesn't help and you can't access any other playerprefs either then it's possible that you don't have access to the place where they should be saved.
You can try running Unity as admin.
'On Windows, PlayerPrefs are stored in the registry under HKCU\Software\[company name]\[product name] key, where company and product names are the names set up in Project Settings.'
So you can also check if you've got something there.


As for Grumpy's script you need to cast the int variable to string, so you probably need to add .ToString() after PlayerPrefs.GetInt("Score").

__________________

http://www.howtomakemobilegames.com/post/twin-stick-shooter-7140396

My second game: Google Play
My first game: Google Play  Amazon iOS

0
J53 Industries

Senior Member
Registered:
Posts: 107
Reply with quote  #16 
This is the only Player Pref that I have in the game. The highscore does not work on a device. What should I check on the registry because I I used to have a script very close to this and it worked so I m wondering if it has something to do with the registry. Thanks
__________________
Check Out My Apps Below

https://play.google.com/store/apps/developer?id=J53+Industries
0
mzr

Senior Member
Registered:
Posts: 222
Reply with quote  #17 
Check out if there is a key in HKCU\Software\[company name]\[product name], it should contain the current values for your playerprefs
__________________

http://www.howtomakemobilegames.com/post/twin-stick-shooter-7140396

My second game: Google Play
My first game: Google Play  Amazon iOS

0
J53 Industries

Senior Member
Registered:
Posts: 107
Reply with quote  #18 
MZR thank you so much for your continued help. I have attached a screenshot of HKCU. I really do not see a key and there are no values set for the data. Any recommendation on what I should do next. Thanks

Untitled.png 


__________________
Check Out My Apps Below

https://play.google.com/store/apps/developer?id=J53+Industries
0
mzr

Senior Member
Registered:
Posts: 222
Reply with quote  #19 
Hmm. I don't have a windows pc to check it right now. I'll take a look tomorrow.
In the meantime you could try adding Debug.Log(PlayerPrefs.GetInt("HighScore").ToString()); just after saving the HighScore to playerprefs.
It should write the number in the console.
If it's not 0 then it is saved, but something removes it.

__________________

http://www.howtomakemobilegames.com/post/twin-stick-shooter-7140396

My second game: Google Play
My first game: Google Play  Amazon iOS

0
J53 Industries

Senior Member
Registered:
Posts: 107
Reply with quote  #20 
MZR I have yet to check the debug.log but what I got thinking is this. To end the game I use this script

function OnCollisionEnter2D (collision : Collision2D) 
{
if (collision.gameObject.tag == "Wall")
{

Application.LoadLevel("0");
}
}


So what I was thinking is no GameOver Function is ever called in this script so maybe that is why the highscore is never saved. Not sure if this is correct but it is just a thought.

Thanks once again

*Quick Update I went back into HKCU and found that something named Score had been saved there. I messed around a little with changing scripts so that on collision function the score would be saved instead of on the GameOver function. This did not set a high score however it did affect the HKCU. I will attach a screenshot. Untitled.png 


__________________
Check Out My Apps Below

https://play.google.com/store/apps/developer?id=J53+Industries
0
mzr

Senior Member
Registered:
Posts: 222
Reply with quote  #21 
Yeah, you definitely should put saving the HighScore there.
You could just call the game over function in there instead of changing the level (since it also does that).
I've sent you a whole unity project earlier, have you tried opening it as it is?
Also the debug.log functionality is super useful, you can put it around the problematic places in your code and it helps immensely to get quick answers.

__________________

http://www.howtomakemobilegames.com/post/twin-stick-shooter-7140396

My second game: Google Play
My first game: Google Play  Amazon iOS

0
mzr

Senior Member
Registered:
Posts: 222
Reply with quote  #22 
So, it looks like all playerprefs are saved in regedit. And yeah, if you use the script you wrote above then you definitely need to put GameOver(); there before changing the level.
__________________

http://www.howtomakemobilegames.com/post/twin-stick-shooter-7140396

My second game: Google Play
My first game: Google Play  Amazon iOS

0
J53 Industries

Senior Member
Registered:
Posts: 107
Reply with quote  #23 
Okay MZR sorry for all the confusion. I have an updated script (I will post it below). However it is not saving the score. The Debug.log is printing saving score just now score is being displayed at the main menu as a high score. How would I go about adding GameOver() in the OnCollision script. Since that might be an easier alternative. Thank you very much for all your help it is greatly appreciated. 


var scoretext : UI.Text;
private var Score : int = 0;
function Awake() 
{
InvokeRepeating("UpdateScore" , .50, .50);
}
  function UpdateScore() 
  {
  Score += 1;
  scoretext.text = "Score: " + Score.ToString();
}
  
function OnCollisionEnter2D (collision : Collision2D) 
{
if (collision.gameObject.tag == "Wall")
{
  if(Score > PlayerPrefs.GetInt("Score")) 
  {
      PlayerPrefs.SetInt("Score", Score);
      PlayerPrefs.Save();
      
      Debug.Log("Score Saved");
    
    
    }
    
    Application.LoadLevel("0");
}
}




__________________
Check Out My Apps Below

https://play.google.com/store/apps/developer?id=J53+Industries
0
mzr

Senior Member
Registered:
Posts: 222
Reply with quote  #24 
From what I can see you now see the correct score in the mainmenu, right?
So, if you want to tweak it a little bit you can remove 

if(Score > PlayerPrefs.GetInt("Score"))
{
PlayerPrefs.SetInt("Score", Score);
PlayerPrefs.Save();
Debug.Log("Score Saved");
}  
Application.LoadLevel("0");

from your oncollision function and paste 'GameOver();' there instead.
And the things you removed you can just put into the game over function.
Or you can just remove the game over function and keep everything else as it is.

__________________

http://www.howtomakemobilegames.com/post/twin-stick-shooter-7140396

My second game: Google Play
My first game: Google Play  Amazon iOS

0
J53 Industries

Senior Member
Registered:
Posts: 107
Reply with quote  #25 
MZR I achieved saving a high score I cannot thank you enough for your continued help on this problem. I am sorry that I did not realize my mistake sooner. Thank you so much once again I really appreciate it!
__________________
Check Out My Apps Below

https://play.google.com/store/apps/developer?id=J53+Industries
0
mzr

Senior Member
Registered:
Posts: 222
Reply with quote  #26 
You're welcome.
__________________

http://www.howtomakemobilegames.com/post/twin-stick-shooter-7140396

My second game: Google Play
My first game: Google Play  Amazon iOS

0
Grumpy

Senior Member
Registered:
Posts: 318
Reply with quote  #27 
Glad you sorted this out, remember to use Debug.Log everywhere when you're trying to fix a bug. It really helps you understand what your code is doing.
0
Previous Topic | Next Topic
Print
Reply

Quick Navigation:

Easily create a Forum Website with Website Toolbox.