Advertisement
gandalfbialy

Untitled

Jul 5th, 2025
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.14 KB | None | 0 0
  1. using TMPro;
  2. using UnityEngine;
  3. using UnityEngine.SceneManagement;
  4.  
  5. public class GameManager : MonoBehaviour
  6. {
  7.     public static GameManager instance;
  8.     public float worldScrollingSpeed = 0.2f;
  9.  
  10.     private float score;
  11.     public TextMeshProUGUI scoreText;
  12.  
  13.     public bool inGame;
  14.     public GameObject resetButton;
  15.  
  16.     private int coins;
  17.     public TMP_Text coinsText;
  18.  
  19.     public Immortality immortality;
  20.  
  21.     public void CoinCollected(int value = 1)
  22.     {
  23.         coins += value;
  24.         PlayerPrefs.SetInt("Coins", coins);
  25.         Debug.Log(coins);
  26.     }
  27.  
  28.     public void ImmortalityCollected()
  29.     {
  30.         if (immortality.isActive)
  31.         {
  32.             CancelInvoke("CancelImmortality");
  33.             CancelImmortality();
  34.         }
  35.  
  36.         immortality.isActive = true;
  37.         worldScrollingSpeed += immortality.GetSpeedBoost();
  38.         Invoke("CancelImmortality", immortality.GetDuration());
  39.     }
  40.  
  41.     private void CancelImmortality()
  42.     {
  43.         immortality.isActive = false;
  44.         worldScrollingSpeed -= immortality.GetSpeedBoost();
  45.     }
  46.  
  47.     void InitializeGame()
  48.     {
  49.         immortality.isActive = false;
  50.  
  51.         inGame = true;
  52.  
  53.         if (PlayerPrefs.HasKey("Coins"))
  54.         {
  55.             coins = PlayerPrefs.GetInt("Coins");
  56.         }
  57.         else
  58.         {
  59.             coins = 0;
  60.             PlayerPrefs.SetInt("Coins", 0);
  61.         }
  62.     }
  63.  
  64.     void Start()
  65.     {
  66.         if (instance == null)
  67.         {
  68.             instance = this;
  69.         }
  70.  
  71.         InitializeGame();
  72.     }
  73.  
  74.     private void FixedUpdate()
  75.     {
  76.         if (!GameManager.instance.inGame)
  77.         {
  78.             return;
  79.         }
  80.  
  81.         score += worldScrollingSpeed * 20 * Time.fixedDeltaTime;
  82.         //worldScrollingSpeed += 0.001f;
  83.         UpdateOnScreenScore();
  84.     }
  85.  
  86.     void UpdateOnScreenScore()
  87.     {
  88.         scoreText.text = score.ToString("0");
  89.         coinsText.text = coins.ToString("0");
  90.     }
  91.  
  92.     public void GameOver()
  93.     {
  94.         inGame = false;
  95.         resetButton.SetActive(true);
  96.     }
  97.  
  98.     public void RestartGame()
  99.     {
  100.         SceneManager.LoadScene(0);
  101.     }
  102. }
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement