Advertisement
gandalfbialy

Untitled

Jun 28th, 2025
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 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 void CoinCollected(int value = 1)
  20. {
  21. coins += value;
  22. PlayerPrefs.SetInt("Coins", coins);
  23. Debug.Log(coins);
  24. }
  25.  
  26. void InitializeGame()
  27. {
  28. inGame = true;
  29.  
  30. if (PlayerPrefs.HasKey("Coins"))
  31. {
  32. coins = PlayerPrefs.GetInt("Coins");
  33. }
  34. else
  35. {
  36. coins = 0;
  37. PlayerPrefs.SetInt("Coins", 0);
  38. }
  39. }
  40.  
  41. void Start()
  42. {
  43. if (instance == null)
  44. {
  45. instance = this;
  46. }
  47.  
  48. InitializeGame();
  49. }
  50.  
  51. private void FixedUpdate()
  52. {
  53. if (!GameManager.instance.inGame)
  54. {
  55. return;
  56. }
  57.  
  58. score += worldScrollingSpeed * 20 * Time.fixedDeltaTime;
  59. //worldScrollingSpeed += 0.001f;
  60. UpdateOnScreenScore();
  61. }
  62.  
  63. void UpdateOnScreenScore()
  64. {
  65. scoreText.text = score.ToString("0");
  66. coinsText.text = coins.ToString("0");
  67. }
  68.  
  69. public void GameOver()
  70. {
  71. inGame = false;
  72. resetButton.SetActive(true);
  73. }
  74.  
  75. public void RestartGame()
  76. {
  77. SceneManager.LoadScene(0);
  78. }
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement