- Home /
How To Show Score On End Game
Hey guys, I need to show score at the end of the game, I want to do this like my pause menu with gui and time.scale codes but the part of my game that handles score has a lot more in it than just score
Essentially I need to make a separate script to handle end game and then use that to call upon the private int score; but only after it's accumulated it's count, I just don't know how to go about it though
here's the part of the script that handles game over:
if (numberOfLives <= 0)
{
HideGroup("GAMEOVER", false);
AudioSource.PlayClipAtPoint(GameOver, Camera.main.transform.position);
respawnTimer = Time.time + gameOverTime;
}
and here's the couple of parts that handle score:
private int score;
void Start () {
Screen.sleepTimeout = SleepTimeout.NeverSleep;
currentLevel = 0;
numberOfLives = 3;
score = 0;
// Game over
HideGroup("GAMEOVER", true);
HideGroup("GUI", false);
respawnTimer = 0;
bPlayerExistedLastUpdate = true;
}
This part is at the very bottom:
public void AddPoints(int points) {
score+=points;
Debug.Log("Score: " + score);
}
and throughout that they get score from destructed objects in other scripts, but the above code is spread out through the one script.
I hope someone can understand what I just said.. Thanks!
Why don't you make your score static.
public static int score;
Then you can get your score from another script like this print($$anonymous$$yScript.score);
Hey thanks, that got rid of my errors, but when I lose all lives the GUI window doesn't pop up, here's the game over script after using the static int
using UnityEngine;
using System.Collections;
public class Over : $$anonymous$$onoBehaviour {
private bool gameOver = true;
public GUISkin guiSkin;
Rect windowRect = new Rect (0, 0, 620, 520);
//private bool toggleTxt = false;
//string stringToEdit = "Text Label";
//float hSliderValue = 0.0f;
//float vSliderValue = 0.0f;
//private float hSbarValuet;
//private float vSbarValue;
//private Vector2 scrollPosition = Vector2.zero;
void Start (){
Time.timeScale = 0.0f;
}
void Update (){
windowRect.x = (Screen.width - windowRect.width) / 2;
windowRect.y = (Screen.height - windowRect.height) / 2;
if (GameLogicScript.numberOfLives <= 0)
{
Time.timeScale = 0.0f;
gameOver = true;
} else {
gameOver = false;
Time.timeScale = 1.0f;
}
}
void OnGUI(){
GUI.skin = guiSkin;
if (gameOver){
windowRect = GUI.Window (0, windowRect, Over$$anonymous$$enu, "Game Over");
}
}
void Over$$anonymous$$enu(int windowOver) {
if (GUI.Button ( new Rect(140,50,340,50), "Restart"))
Application.LoadLevel("TestScene1");
if (GUI.Button ( new Rect(140,120,340,50), "$$anonymous$$ain $$anonymous$$enu"))
Application.LoadLevel ("$$anonymous$$enu");
GUI.Button ( new Rect(140,190,340,50), "Options");
GUI.Button ( new Rect(140,260,340,50), "Score: " +GameLogicScript.score);
if (GUI.Button ( new Rect(140,330,340,50), "Quit"))
Application.Quit ();
}
}
if you need to see the other script, I will post it.
Answer by Gnemlock · Mar 16, 2014 at 12:39 PM
I would have a GUIText in your scene wherever you want the score displayed, and set it up with a generic score display as the text so you can see it as it will display in your scene.
Whatever object holds your score script ("gameController"), have this code in it
public GUItext scoreText
void Start()
{
scoreText.text=""; //reset your score text to start empty/invisible
}
Then, when you handle the gameover you would include
scoreText.text="Final Score: " + score;
Then when you reset your score you would just reset the scoreText.text with it.
Thanks for this, with some fiddling around with it I was able to use it, but not for what I asked for. I ended up getting the GUI to display correctly but the way I did it made me get rid of my other GUI elements that displayed score while the player was playing, this fixed it.
But now I have a problem of when I have my end game gui pop up, the game will pause (because I'm using timescale) and then the user can push the GUI buttons and either restart, go to main menu or quit. But if the user presses anything but the GUI, the game will reset in the background while the gui is still there.
What I need is to be able to disable the users ability to touch anything but the GUI window & GUI buttons, any ideas?
Your answer
Follow this Question
Related Questions
How can i destroy a collider on collision? 2 Answers
Modified Roll a Ball Tutorial,Modified Roll a Ball tutorial question 1 Answer
Show Current Score on GAMEOVER Scene 0 Answers
Highscore Save 0 Answers
OnControllerColliderHit counting twice towards score 0 Answers