- Home /
Score script not working , Wanting to show high score but can't. please help !!
My game goes with level and score I calculated with how much I go in z-direction . the following are scripts I used using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CoreScore : MonoBehaviour { public Transform player; public static float x = 0f;
public void Update()
{
x += player.position.z;
}
} using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement ;
public class Score : MonoBehaviour {
public Transform player;
public Text scoreText;
public Text highScore;
private void Start()
{
highScore.text = PlayerPrefs.GetFloat("Highscore", 0).ToString("0");
CoreScore.x = PlayerPrefs.GetFloat("Highscore");
}
// Update is called once per frame
void Update()
{
CoreScore.x = player.position.z;
scoreText.text = CoreScore.x.ToString("0");
highScore.text = CoreScore.x.ToString("0");
/* int buildindex = SceneManager.GetActiveScene().buildIndex;
if (buildindex == 1)
{
scoreText.text = CoreScore.x.ToString("0");
}
if (buildindex >1||buildindex < 10)
{
scoreText.text = (CoreScore.x).ToString("0");
}
*/
if (CoreScore.x > PlayerPrefs.GetFloat("HighScore",0) )
{
PlayerPrefs.SetFloat("HighScore", CoreScore.x);
highScore.text = CoreScore.x.ToString("0");
}
}
} please help me with this script thanks in advance :)
Please fix your formatting so all code is correctly placed in the right code block. This will prevent your question from being skipped by some users. I wanted to fix your formatting myself but I think I don't have enough points yet.
Also I see some commented code. What has the scene index to do with your problem. Please avoid adding unnecessary code. That way you keep your questions shorter and easier to read.
I was actually trying different way and commented it and then forgot to remove it.
Sorry, just trying to clarify, what exactly is the issue with this? How isn't the code working (I haven't tested it or anything, just asking)?
It's that after every scene score resets to 0 . And it doesn't add last score in new scene
Answer by Stratosome · Aug 14, 2018 at 07:16 PM
Heyo,
Sorry I didn't respond sooner. Needed to sleep hah. Anyway, I think I know your problem. Up inside of your Start method, you are looking for a PlayerPref variable called 'Highscore' but below you are setting the PlayerPref variable 'HighScore', so that's a problem. They need to be called the same! I didn't see it right away and was trying to make a simple example of the whole thing just to see if I could get it to work. I'm pretty sure that's just your error, but if you'd like to see my version of the code anyway, here ya go:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Score : MonoBehaviour {
public static float x = 0.0f; // Your actual score
private float lastScore = 0.0f; // The additional score from the last scene
public Transform player;
public string scoreText;
public string highScore;
private void Start() {
// When scene is loaded, grab highscore from previous scene
x = PlayerPrefs.GetFloat("Highscore", 0);
lastScore = x;
highScore = x.ToString("0");
scoreText = highScore;
}
void Update() {
if (player) {
// Set score
x = player.position.z + lastScore;
// Update text
scoreText = x.ToString("0");
highScore = x.ToString("0");
// Save new highscore if x is greater than the last
if (x > PlayerPrefs.GetFloat("Highscore", 0)) {
PlayerPrefs.SetFloat("Highscore", x);
}
}
}
}
I thank you for your effort. Thanks for solving my problem
'Course! Happy to help ya. At least it was just a simple little problem hah.
one more problem is there , I wonder if u can help me . $$anonymous$$y android device recently upgraded to android oreo(8.0) . Before update my game was running on my device but after update , the game icon changed with unity logo icon and when I start the game, the message appears the game stopped working . I don't know why this error is occurring even though I have set to build at highest installed OS.
Answer by ShadowUser19 · Aug 12, 2018 at 08:55 AM
I haven't used PlayerRefs yet, but I think you are supposed to use the Save method after making modifications:
if (CoreScore.x > PlayerPrefs.GetFloat("HighScore",0) )
{
PlayerPrefs.SetFloat("HighScore", CoreScore.x);
PlayerPrefs.Save();
highScore.text = CoreScore.x.ToString("0");
}
Also make sure the script is attached to a GameObject in Unity & that all the fields are set in the unity inspector.
No use, it did not work. I want it like as the scene changes after completing a level ,the last score adds to the next score which will update with player's z- position and when game is quit the high score is stored in the highscore variable
Your answer
Follow this Question
Related Questions
How to access my highscore from another scene ? 1 Answer
whats wrong with my high score code? 1 Answer
How can i call this void once 2 Answers
help understand @if false 1 Answer
problem when turning the object 0 Answers