Advertisement
Nemomen101

First Game Score Script

Jan 18th, 2025
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | Source Code | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using TMPro;
  4.  
  5. public class Scorer : MonoBehaviour
  6. {
  7.  
  8. int hits = -1;
  9. public TMP_Text hitCounterText;
  10.  
  11. void Start()
  12. {
  13. if (hitCounterText != null)
  14. {
  15. hitCounterText.text = "Hits: 0";
  16. }
  17. else
  18. {
  19. Debug.LogWarning("HitCounterText is not assigned in the Inspector!");
  20. }
  21. }
  22.  
  23.  
  24.  
  25.  
  26.  
  27. private void OnCollisionEnter(Collision other)
  28. {
  29.  
  30.  
  31. if (other.gameObject.tag != "Hit")
  32. {
  33. hits++;
  34. Debug.Log("You have crashed this many times: " + hits);
  35.  
  36.  
  37.  
  38. if (hitCounterText != null)
  39. {
  40. hitCounterText.text = "Hits: " + hits;
  41.  
  42. }
  43. }
  44.  
  45.  
  46. }
  47.  
  48. }
  49.  
Tags: Score script
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement