- Home /
Displaying highscore in the main menu scene
I need to display the highscore in the main menu and the score is gained in another scene. The script where the points are gained is :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class ballandscore : MonoBehaviour {
public int timeLeft = 30;
public Text countdownText;
public int Score;
public Text ScoreText;
public int scoreresult;
public Text ScoreResult;
public bool scoresult;
public GameObject Player1;
void Start () {
StartCoroutine ("LoseTime");
Score = 0;
SetScoreText ();
scoreresult = 0;
scoresult = false;
ScoreResult.text = "";
}
void Update () {
countdownText.text = ("Time left : " + timeLeft);
if(timeLeft <= 0){
StopCoroutine("LoseTime");
GameOver ();
}
}
IEnumerator LoseTime()
{
while (true) {
yield return new WaitForSeconds (1f);
timeLeft = timeLeft - 1;
}
}
void OnCollisionEnter2D(Collision2D colliii){
if(colliii.gameObject.tag == "ball"){
Destroy(colliii.gameObject);
Score = Score + 3;
scoreresult = scoreresult + 3;
SetScoreText ();
timeLeft = timeLeft + 2;
PlayerPrefs.SetInt ("High Score", scoreresult);
}
}
void SetScoreText ()
{
ScoreText.text = Score.ToString ();
}
public void GameOver ()
{
ScoreResult.text = "Your Score Is " + scoreresult.ToString();
Destroy (Player1);
countdownText.text = "";
ScoreText.text = "";
scoresult = true;
}
}
The script attached to an empty gameobject located in the main menu scene is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HighscoreScript : MonoBehaviour {
public Text HighscoreText;
public int HigherScore = 0;
ballandscore theScore;
public int truescore = 0;
void Start ()
{
theScore = GetComponent<ballandscore> ();
truescore = theScore.scoreresult;
if (truescore > HigherScore) {
HigherScore = truescore;
}
haighScore ();
}
void Update(){
}
void haighScore()
{
HighscoreText.text = "Highscore : " + PlayerPrefs.GetInt ("High Score",truescore);
}
}
I did a lot of research but most of the things I found were outdated. Thank you for helping.
The Highscore does not show up in the main menu.It stays as "New Text"
and you remember to drag your Score Text object to the Inspector public var?
Answer by OneCept-Games · Jan 08, 2018 at 05:15 PM
theScore is null on return of GetComponent(). Do you have a ballscore script on your GameObject from where you call GetComponent()?
Well , the ballandscore script is in the other scene , attached to the player.In the main menu scene , there is only the empty object which has the HighscoreScript attached to it. So I need to attach the ballandscore script on this empty object too ?
I think I need to call the scoreresult variable from the ballandscore script to the highscorescript , but I don't know how to do that since they are in different scenes and scripts , and attached to different GameObjects.
Ok thanks to your idea I fixed it.I made the gameobject controlled by the player a prefab and put it in the main menu as a decoration so that the ballandscore script was in the main menu scene,to be able to get the component in the Highscorescript.
Glad you fixed it. I guess you do not need all methods in the ballonscore in your main menu, so you should either try to make a script that only manages Set- and Get to/from PlayerPrefs, "High Score" key. But you can of course always attach the ballonscore to an Empty GameObject in your $$anonymous$$ain $$anonymous$$enu scene and leave the variables empty, and then in your code check if they are Null and then do not use them.
But most important that you have fixed it and can continue your development. Good luck with your project!
Your answer
Follow this Question
Related Questions
How to set and save individual highscores for each scene using serialization? 3 Answers
Saving current scene in another scene during gameplay? 1 Answer
options menu UI doesnt save when switching scenes 1 Answer
my text size changes didnt saved by slider 1 Answer
Save Game Sceenes user quit aplication, Can I save the scene as a whole while leaving the game? 0 Answers