Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.SceneManagement;
- public class ScoreScript : MonoBehaviour
- {
- public int score = 0;
- public Text scoreText;
- public Text timerText;
- // Get gameOver from timer (if not points)
- public bool gameOverByTimer = false;
- public int scoreToGameOver = 10;
- public float timeToGameOver = 10;
- public float timer;
- int scoreThisLevel = 0;
- void Start ()
- {
- score = PlayerPrefs.GetInt("Score");
- // Set timer
- timer = timeToGameOver;
- // Call adjust score without adjusting (0)
- AdjustScore(0);
- }
- void Update()
- {
- // Timer counts down
- timer -= Time.deltaTime;
- timerText.color = Color.yellow;
- if (gameOverByTimer)
- timerText.text = "Timer: " + timer.ToString("F0"); // Update timer text formatted to no decimals
- else
- timerText.text = "Score: " + scoreThisLevel + " out of " + scoreToGameOver;
- // If the timer is 0 load next scene
- if(timer <= 0 && gameOverByTimer)
- {
- LoadNextLevel();
- }
- else if (timer <= 3 && gameOverByTimer)
- {
- timerText.color = Color.red;
- }
- else if (scoreThisLevel >= scoreToGameOver && !gameOverByTimer)
- {
- LoadNextLevel();
- }
- // If pressed Esc
- if(Input.GetKeyDown(KeyCode.Escape))
- {
- // Load current scene (restart)
- SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
- }
- }
- public void LoadNextLevel()
- {
- // Saves score
- PlayerPrefs.SetInt("Score", score);
- // Loads next scene
- SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex+1);
- }
- public void GameOver()
- {
- PlayerPrefs.SetInt("Score", score);
- SceneManager.LoadScene(SceneManager.sceneCountInBuildSettings-1);
- }
- public void AdjustScore(int adj)
- {
- // Add the adjustment to total score / score this lvl
- score += adj;
- scoreThisLevel += adj;
- // Update score text
- scoreText.text = "Score: " + score;
- }
- void OnApplicationQuit()
- {
- PlayerPrefs.SetInt("Score", 0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement