SHARE
TWEET

ScoreScript

a guest Nov 28th, 2018 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.SceneManagement;
  6.  
  7. public class ScoreScript : MonoBehaviour
  8. {
  9.     public int score = 0;
  10.     public Text scoreText;
  11.     public Text timerText;
  12.  
  13.     // Get gameOver from timer (if not points)
  14.     public bool gameOverByTimer = false;
  15.     public int scoreToGameOver = 10;
  16.     public float timeToGameOver = 10;
  17.     public float timer;
  18.  
  19.     int scoreThisLevel = 0;
  20.  
  21.     void Start ()
  22.     {
  23.         score = PlayerPrefs.GetInt("Score");
  24.  
  25.         // Set timer
  26.         timer = timeToGameOver;
  27.         // Call adjust score without adjusting (0)
  28.         AdjustScore(0);
  29.     }
  30.    
  31.     void Update()
  32.     {
  33.         // Timer counts down
  34.         timer -= Time.deltaTime;
  35.         timerText.color = Color.yellow;
  36.        
  37.         if (gameOverByTimer)
  38.             timerText.text = "Timer: " + timer.ToString("F0"); // Update timer text formatted to no decimals
  39.         else
  40.             timerText.text = "Score: " + scoreThisLevel + " out of " + scoreToGameOver;
  41.  
  42.         // If the timer is 0 load next scene
  43.         if(timer <= 0 && gameOverByTimer)
  44.         {
  45.             LoadNextLevel();
  46.         }
  47.         else if (timer <= 3 && gameOverByTimer)
  48.         {
  49.             timerText.color = Color.red;
  50.         }
  51.         else if (scoreThisLevel >= scoreToGameOver && !gameOverByTimer)
  52.         {
  53.             LoadNextLevel();
  54.         }
  55.  
  56.         // If pressed Esc
  57.         if(Input.GetKeyDown(KeyCode.Escape))
  58.         {
  59.             // Load current scene (restart)
  60.             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
  61.         }
  62.     }
  63.  
  64.     public void LoadNextLevel()
  65.     {  
  66.         // Saves score
  67.         PlayerPrefs.SetInt("Score", score);
  68.            
  69.         // Loads next scene
  70.         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex+1);
  71.     }
  72.  
  73.     public void GameOver()
  74.     {
  75.         PlayerPrefs.SetInt("Score", score);
  76.         SceneManager.LoadScene(SceneManager.sceneCountInBuildSettings-1);
  77.     }
  78.  
  79.     public void AdjustScore(int adj)
  80.     {
  81.         // Add the adjustment to total score / score this lvl
  82.         score += adj;
  83.         scoreThisLevel += adj;
  84.  
  85.         // Update score text
  86.         scoreText.text = "Score: " + score;
  87.     }
  88.  
  89.     void OnApplicationQuit()
  90.     {
  91.         PlayerPrefs.SetInt("Score", 0);
  92.     }
  93. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top