- Home /
 
Showing score with GUI
Hello everyone i want to show the score when map changed here is my full script ;
 using UnityEngine;
 using System.Collections;
 
 public class Fuel : MonoBehaviour {
 
 
     public GUIStyle penguin1;
     public GUIStyle penguin2;
     public GUIStyle penguin3;
     public GUIStyle penguin4;
     public GUIStyle penguin5;
 
     public float fuel = 20;
     private int score = 0;
 
     
 
     void Update() {
         fuel -= 1 * Time.deltaTime; 
 
         
         if (fuel < 0) {
 
         }
         
         if (fuel > 40 ) {
             fuel = 40;
         }
         
         if (fuel < -2) {
             Application.LoadLevel("gameOver");
         }
         
         print (fuel);
     
     }
     
 
 
     void OnGUI () {
 
 
         if (fuel >= 30 && fuel <= 40) 
             GUI.Box (new Rect (60,18,65,45), "" , penguin1);
         
         
         if (fuel >= 20 && fuel <= 30)
             GUI.Box (new Rect (60,18,65,45), "" , penguin2);  
         
 
         if (fuel >= 10 && fuel <= 20)
             GUI.Box (new Rect (60,18,65,45), "" , penguin3);                          
         
         
         if (fuel >= 0 && fuel <= 10)
             GUI.Box (new Rect (60,18,65,45), "" , penguin4);
 
 
         if (fuel < 0)
             GUI.Box (new Rect (60,18,65,45), "" , penguin5);
 
 
 
         if (Application.loadedLevelName == "gameOver") {
 
         
         }
     }
     
     void OnTriggerEnter (Collider other) {
         fuel +=8; 
             score +=1;
     }
     
 
     void OnTriggerExit (Collider other)  {
         Destroy(other.gameObject);
         
     }
 }
 
               When map changed to gameOver i want to show the score is it possible with gui ?
Thanks..
Answer by Jeric-Miana · Sep 12, 2014 at 02:41 PM
Use this one bro, hope this helps
     void Update()
     {
      if (fuel < -2) 
     {
     Application.LoadLevel("gameOver");
     PlayerPrefs.SetInt("SaveScore",score);
     // save your current score 
     }
     }
 
               Attach this script in your next scene
     void OnGUI()
     {
     GUI.Label (Rect (10, 10, 100, 20), "Score" + PlayerPrefs.GetInt("SaveScore"));
 //display saved score 
     }
 
              Thanks for answer i did the first part but in the second part when i create a new script and paste your code it gives me error ;
"Expression denotes a type', where a variable', value' or method group' was expected." 
Oh i fixed it. It's supposed to be (new Rect (10, 10, 100, 20) Thanks for answer :)
Answer by gnirts · Sep 12, 2014 at 03:42 PM
Hi,
I don't know how you manage the logic of your game (pre-made levels, single level with procedurally generation etc.).
In my game don't use PlayerPrefs for some reasons, so when I have to display some score I save a global var in the namespace of the project (I have a public static class called GlobalVariables), then I have my ProgrammaticGUI class which contains all general GUI info and rules.
In your score code I would put... let's say something like:
void OnTriggerEnter (Collider other) {
    GlobalVariables.Fuel +=8;
    GlobalVariables.GlobalScore +=1;
}
 
                
               With a couple of booleans you manage wether to show or not the score (of your level or global score etc.)
Something like
void OnGUI()
{
    if(ShowGlobalScore){
        GUI.Label (MyRect, GlobalVariables.GlobalScoreVar);
    }
}
 
                
              Your answer
 
             Follow this Question
Related Questions
Need help displaying score (JavaScript) 1 Answer
Adding score when enemy dies (Errors) 2 Answers
Trouble displaying a variable as a GUI from a different script 1 Answer
Scoring with positions 1 Answer
Count the Score 1 Answer