- Home /
How do I keep the highest scoring value for a game session?
Hi guys.
I have a problem with my little scoring system. Wonder if u guys can help me out. Once again, I've been following the tutorials on catlikecoding.com to sharpen up my C# skills.
My scoring system is based on how far a player has traveled (like the function used in the tutorial)
In my Runner.cs Script:
distanceTraveled = transform.localPosition.x;
GUIManager.SetDistance (distanceTraveled);
Now I have managed to add a new variable for high score on a guitext which displays my last score achieved after the game over screen and shows up when u reset the scene. But, here's my problem and concern : Whenever the game ends now and the scene resets it uses the previous score achieved and doesn't keep the highest value achieved, meaning I might was well rename it to previous score instead of high score.
In my Runner.cs Script for setting score:
highScore = distanceTraveled;
If (distanceTraveled > highScore) {
GUIManager.SetScore (highScore);
}
else if (distanceTraveled < highScore) {
//(I haven't set anything here as I'm not sure wtf I can use here, and believe me I have tried)
}
In my GUIManager.cs Script:
public static void SetScore (float highScore) {
instance.highScore.text = highScore.ToString ("f0");
}
So my question is, how do I keep the highest distance traveled value in my high score variable for the remainder of that game session?
PS: Once again, if I don't make any sense, just curse me. Have to do this from my phone again seeing as this god forsaken country haven't installed my internet line yet.
Thanks in advance.
Hi @Blood_Rush it's important to format your code correctly so that we can read your question more easily. I've helped out for this question but try to bear it in $$anonymous$$d for the future.
Sorry about that. I'm using this dumbass blackberry phone of $$anonymous$$e to post things, and sometimes I'm not near my pc to remember what my code looks like.
Thanks for helping me out with that.
Answer by KellyThomas · Jan 17, 2014 at 10:54 PM
This test will always fail. By assigning the same value to two different variables it is impossible for one to be greater than the other.
highScore = distanceTraveled;
if (distanceTraveled > highScore) {
GUIManager.SetScore (highScore);
}
Instead use this:
if (distanceTraveled > highScore) {
highScore = distanceTraveled;
GUIManager.SetScore (highScore);
}
Just be sure to initialize highscore
(to 0
or the saved highscore) in Start()
/Awake()
or somewhere else outside of the update loop.
Ok, thanks. Let me just pick up my brain from the floor. Haha. Nah, it's all good. I understand some of the logic. I'm just playing it in my head how to incorporate all of it into the current code. But thanks for that. I appreciate the help. I will get back to you if I require assistance.
Answer by fuzzysmurf · Jan 17, 2014 at 10:57 PM
Im not to sure how to do this in Unity, I believe you just transfer the int (or float) between scripts. otherwise, you can create a CSV file as an alternative (using File.writeLine(location, string)), and it will always contain the values for you. if you do try this, you will need "Using System.IO", although I would only recommend this as a last resort. I'm sure Unity has a built in method, or an alternative to approaching this.
I don't want to get too technical as this is but a basic game and also my first time getting this far developing sumthing from scratch (with a little bit of help from the catlikecoding.com tutorial.) $$anonymous$$ost of the games I made was for friends and was just a learning experience, but I just dunked a fps controller in, built my environment and modelling architecture. I'm not a strong programmer, although I have done a little bit of php. The PlayerPrefs seems like a good idea, I just have to basically instance my travel distance to it. So thanks for that. Although its gonna take me a while to figure it out.
Your answer
Follow this Question
Related Questions
SHMUP - Scoring and Scripting Help 1 Answer
How to save score for survival time? 1 Answer
How to set your numbers to expand the opposite direction? 1 Answer
Score System? 0 Answers