- Home /
Show score on gameover screen?
Hello Unity Answers,
I'm trying to make a simple gameover screen with the score shown to the player.
I have created a new scene and copied everything from the main scene into it and repositioned the score text but the score doesn't update.
All ideas & suggestions welcome.
using System;
using System.Collections.Generic;
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Question10 : MonoBehaviour {
private Rect windowRect = new Rect (500, 100, 400, 200); //Window size
public bool question10;
private int count;
public Text countText;
private bool showTimer = true;
private float Timer = 10f;
void start()
{
count = 0;
SetCountText ();
}
void FixedUpdate(){
if (showTimer == true) {
Timer -= Time.deltaTime;
}
if (Timer <= 0f) {
showTimer = false;
Timer = 10f;
Destroy (this.gameObject);
Application.LoadLevel (Application.loadedLevel);
}
}
void OnGUI()
{
windowRect = GUI.Window (0, windowRect, WindowFunction, "Ebola Quiz Island"); //window on screen
}
void WindowFunction (int windowID)
{
// Draw any Controls inside the window here
GUI.Label(new Rect (30, 25, 400, 50), " How many proven treatments are there for Ebola?"); // Question
if (GUI.Button (new Rect (20, 100, 100, 30), "None"))
{
Destroy (this.gameObject);
count = count + 10;
SetCountText ();
Application.LoadLevel("Gameover");
}
if (GUI.Button (new Rect (280, 100, 100, 30), "One"))
{
Destroy (this.gameObject);
Application.LoadLevel("Gameover");
}
if (GUI.Button (new Rect (20, 150, 100, 30), "Two"))
{
Destroy (this.gameObject);
Application.LoadLevel("Gameover");
}
if (GUI.Button (new Rect (280, 150, 100, 30), "Three"))
{
Destroy (this.gameObject);
Application.LoadLevel("Gameover");
}
if (showTimer == true)
{
GUI.Label (new Rect (175, 50, 200, 50), "Timer: " + Timer.ToString ("F0"));
}
}
void SetCountText()
{
ValuesHolder.answersCount++;
countText.text = "Score: " + ValuesHolder.answersCount.ToString();
}
}
using UnityEngine;
using System.Collections;
internal class ValuesHolder
{
internal static int answersCount = 0;
}


When the player clicks the None the scoreboard scene ( Application.LoadLevel("Gameover");) will be loaded but the score won't be updated? :(
You should change start() to Start(). I'm also not sure why you are using the internal keyword ins$$anonymous$$d of just using public.
Is something in the Gameover scene reseting the score and updating the display?
Is something in the Gameover scene reseting the score and updating the display?
No because i have positioned the fpc at the end of the level in the gameover scene.
The only way the score updates is if the player answers a question correctly as i have stored the score in one of the buttons, so when the button is clicked the score updates which happens in the Game scene.
The code I have in my Question10 class is exactly the same for all the other Question classes, so I have Question1, Question2,etc. with exactly the same code accept for line 52, 58, 64, 70 which change the scene.
Not sure how to basically answer all the questions regardless if you got them right or wrong and then display the score in the gameover scene?
Answer by hbalint1 · Apr 14, 2015 at 07:47 AM
Because You set the text first in line 51 and change the scene in 52 which clears the data. So when the new scene is loaded, you should load the points into your game label. In the GameOver scene you should make a new gameobject (empty) and give it a script. In the script you can call a function, like your SetCountText, but without incrementing the count.
What i don't get: Why do you have a local count variable, if you use only the static Valuesholder.answersCount?
Your answer
Follow this Question
Related Questions
Keeping a score from one scene to another? 2 Answers
Play Sound on Score 2 Answers
High score in standalone 1 Answer
PlayerPrefs Highscore problem 1 Answer
can xml be used in anrdoid?? 1 Answer