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

Space Shooter Highscore GUI scripting help C#

Discussion in 'Scripting' started by IDontCare85060, Feb 17, 2015.

  1. IDontCare85060

    IDontCare85060

    Joined:
    Jan 23, 2015
    Posts:
    28
    After maybe a hour of creating a script for the space shooter tutorial that unity made i finally got it to the point were i don't have any errors but i am confused on how i can make it work with the Done_GameController Script so i can have a GUI text pop-up by the score and display the high score. So is there a way that i can fix it or that i could combine it.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Done_GameController : MonoBehaviour
    6. {
    7.     public GameObject hazard;
    8.     public Vector3 spawnValues;
    9.     public int hazardCount;
    10.     public float spawnWait;
    11.     public float startWait;
    12.     public float waveWait;
    13.    
    14.     public GUIText scoreText;
    15.     public GUIText restartText;
    16.     public GUIText gameOverText;
    17.    
    18.     private bool gameOver;
    19.     private bool restart;
    20.     private int score;
    21.     private HighScore HighScore;
    22.  
    23.     void Start ()
    24.     {
    25.         GameObject HighScoreObject = GameObject.FindWithTag ("GameController");
    26.         if (HighScoreObject != null)
    27.         {
    28.             HighScore = HighScoreObject.GetComponent <HighScore>();
    29.         }
    30.         if (HighScore == null)
    31.         {
    32.             Debug.Log ("Cannot find 'HighScore' script");
    33.         }
    34.     }
    35.  
    36.     void OnStart ()
    37.     {
    38.         gameOver = false;
    39.         restart = false;
    40.         restartText.text = "";
    41.         gameOverText.text = "";
    42.         score = 0;
    43.         UpdateScore ();
    44.         StartCoroutine (SpawnWaves ());
    45.     }
    46.    
    47.     void Update ()
    48.     {
    49.         if (restart)
    50.         {
    51.             if (Input.GetKeyDown (KeyCode.R))
    52.             {
    53.                 Application.LoadLevel (Application.loadedLevel);
    54.             }
    55.         }
    56.     }
    57.    
    58.     IEnumerator SpawnWaves ()
    59.     {
    60.         yield return new WaitForSeconds (startWait);
    61.         while (true)
    62.         {
    63.             for (int i = 0; i < hazardCount; i++)
    64.             {
    65.                 Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
    66.                 Quaternion spawnRotation = Quaternion.identity;
    67.                 Instantiate (hazard, spawnPosition, spawnRotation);
    68.                 yield return new WaitForSeconds (spawnWait);
    69.             }
    70.             yield return new WaitForSeconds (waveWait);
    71.            
    72.             if (gameOver)
    73.             {
    74.                 restartText.text = "Press 'R' for Restart";
    75.                 restart = true;
    76.                 break;
    77.             }
    78.         }
    79.     }
    80.    
    81.     public void AddScore (int newScoreValue)
    82.     {
    83.         score += newScoreValue;
    84.         UpdateScore ();
    85.     }
    86.    
    87.     void UpdateScore ()
    88.     {
    89.         scoreText.text = "Score: " + score;
    90.     }
    91.    
    92.     public void GameOver ()
    93.     {
    94.         gameOverText.text = "Game Over!";
    95.         gameOver = true;
    96.     }
    97. }
    Highscore script

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.    
    4. public class HighScore: MonoBehaviour
    5.     {
    6.     public bool levelComplete;
    7.     public string highscorePos;
    8.     public int score;
    9.     public int temp;
    10.     private GameController gameController;
    11.  
    12.     void Start ()
    13.     {
    14.         GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
    15.         if (gameControllerObject != null)
    16.         {
    17.             gameController = gameControllerObject.GetComponent <GameController>();
    18.         }
    19.         if (gameController == null)
    20.         {
    21.             Debug.Log ("Cannot find 'GameController' script");
    22.         }
    23.     }
    24.        
    25.     void OnStart ()
    26.         {
    27.             score=0;
    28.         }
    29.        
    30.         void OnLevelComplete ()
    31.         {
    32.             levelComplete=true;
    33.             score=10000; //values from your scoring logic
    34.             for(int i=1; i<=5; i++) //for top 5 highscores
    35.             {
    36.                 if(PlayerPrefs.GetInt("highscorePos"+i)<score)     //if cuurent score is in top 5
    37.                 {
    38.                     temp=PlayerPrefs.GetInt("highscorePos"+i);     //store the old highscore in temp varible to shift it down
    39.                     PlayerPrefs.SetInt("highscorePos"+i,score);     //store the currentscore to highscores
    40.                     if(i<5)                                        //do this for shifting scores down
    41.                     {
    42.                         int j=i+1;
    43.                         PlayerPrefs.SetInt("highscorePos"+j,temp);  
    44.                     }
    45.                 }
    46.             }
    47.         }
    48.        
    49.         void OnGUI()
    50.         {
    51.             if(levelComplete)
    52.             {
    53.                 for(int i=1; i<=5; i++)
    54.                 {
    55.                     GUI.Box(new Rect(100, 75*i, 150, 50), "Pos "+i+". "+PlayerPrefs.GetInt("highscorePos"+i));
    56.                 }
    57.             }
    58.         }
    59.     }
    Thanks in advance.
     
  2. IDontCare85060

    IDontCare85060

    Joined:
    Jan 23, 2015
    Posts:
    28