Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Show final score on gameover screen?

Discussion in 'Scripting' started by RKM_91, Apr 14, 2015.

  1. RKM_91

    RKM_91

    Joined:
    Oct 27, 2013
    Posts:
    28
    Hi,

    I'm trying to make a simple gameover screen with the score shown to the player.
    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 changes 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?

    All ideas & suggestions welcome.


    Code (CSharp):
    1. using System;
    2.     using System.Collections.Generic;
    3.     using UnityEngine;
    4.     using System.Collections;
    5.     using UnityEngine.UI;
    6.  
    7.     public class Question10 : MonoBehaviour {
    8.    
    9.         private Rect windowRect = new Rect (500, 100, 400, 200); //Window size
    10.         public bool question10;
    11.         private int count;
    12.         public Text countText;
    13.  
    14.         private bool showTimer = true;
    15.         private float Timer = 10f;
    16.  
    17.         void start()
    18.         {
    19.             count = 0;
    20.             SetCountText ();
    21.         }
    22.  
    23.         void FixedUpdate(){
    24.             if (showTimer == true) {
    25.                 Timer -= Time.deltaTime;
    26.             }
    27.        
    28.             if (Timer <= 0f) {
    29.                 showTimer = false;
    30.                 Timer = 10f;
    31.                 Destroy (this.gameObject);
    32.                 Application.LoadLevel (Application.loadedLevel);
    33.             }
    34.         }
    35.    
    36.         void OnGUI()
    37.         {
    38.             windowRect = GUI.Window (0, windowRect, WindowFunction, "Ebola Quiz Island"); //window on screen
    39.         }
    40.    
    41.    
    42.         void WindowFunction (int windowID)
    43.         {
    44.             // Draw any Controls inside the window here
    45.             GUI.Label(new Rect (30, 25, 400, 50), " How many proven treatments are there for Ebola?"); // Question
    46.        
    47.             if (GUI.Button (new Rect (20, 100, 100, 30), "None"))
    48.             {
    49.                 Destroy (this.gameObject);
    50.                 count = count + 10;
    51.                 SetCountText ();
    52.                 Application.LoadLevel("Gameover");
    53.             }
    54.  
    55.             if (GUI.Button (new Rect (280, 100, 100, 30), "One"))
    56.             {
    57.                 Destroy (this.gameObject);
    58.                 Application.LoadLevel("Gameover");
    59.             }
    60.  
    61.             if (GUI.Button (new Rect (20, 150, 100, 30), "Two"))
    62.             {
    63.                 Destroy (this.gameObject);
    64.                 Application.LoadLevel("Gameover");
    65.             }
    66.  
    67.             if (GUI.Button (new Rect (280, 150, 100, 30), "Three"))
    68.             {
    69.                 Destroy (this.gameObject);
    70.                 Application.LoadLevel("Gameover");
    71.             }
    72.  
    73.             if (showTimer == true)
    74.             {
    75.                 GUI.Label (new Rect (175, 50, 200, 50), "Timer: " + Timer.ToString ("F0"));
    76.             }
    77.  
    78.  
    79.         }
    80.  
    81.             void SetCountText()
    82.             {
    83.                 ValuesHolder.answersCount++;
    84.                 countText.text = "Score: " + ValuesHolder.answersCount.ToString();
    85.             }
    86.    
    87.     }

    Code (CSharp):
    1. using UnityEngine;
    2.     using System.Collections;
    3.  
    4.     internal class ValuesHolder
    5.     {
    6.         internal static int answersCount = 0;
    7.     }








    **When the player clicks the None, the scoreboard scene ( Application.LoadLevel("Gameover");) will be loaded but the score won't be update? :( **


    I have made a new game object in the gameover scene and attached a script called final score and have called the SetCountText function but it doesn't update the score at the end of the game.

    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Collections;
    5. using UnityEngine.UI;
    6.  
    7. public class finalscore : MonoBehaviour {
    8.  
    9.     private int count;
    10.     public Text countText;
    11.  
    12.  
    13.     void start()
    14.     {
    15.         count = 0;
    16.         SetCountText ();
    17.     }
    18.  
    19.  
    20.     void SetCountText()
    21.     {
    22.         ValuesHolder.answersCount++;
    23.         countText.text = "Score: " + ValuesHolder.answersCount.ToString ();
    24.     }
    25. }
    26.  
     
  2. dvirus1023

    dvirus1023

    Joined:
    Mar 4, 2013
    Posts:
    51
    Create an empty game object that you attach a script to and have it set to not destroy on load. Then you increment a value on that script for each correct answer. Then on your final screen you will pull the value from that gameObject>script.