Advertisement
Guest User

adding and decreasing life

a guest
Aug 15th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. //script reference
  2.  
  3. [SerializeField] GameObject spawner;
  4. [SerializeField] MovePlayer movePlayer;
  5. [SerializeField] AudioClip winningSFX;
  6.  
  7. // cache references
  8.  
  9. [SerializeField] int ballsLeft;
  10. [SerializeField] int livesLeft = 3;
  11. [SerializeField] GameObject livesImg;
  12. [SerializeField] GameObject livesImg2;
  13. [SerializeField] GameObject livesImg3;
  14. [SerializeField] GameObject livesImg4;
  15. [SerializeField] GameObject livesImg5;
  16. [SerializeField] GameObject winScreen;
  17. [SerializeField] GameObject loseScreen;
  18. [SerializeField] TextMeshProUGUI ballsLeftText;
  19. [SerializeField] GameObject spawnerParent;
  20. [SerializeField] GameObject spawnerParent2;
  21. [SerializeField] GameObject spawnerParent3;
  22.  
  23. // Start is called before the first frame update
  24. void Start()
  25. {
  26. var movePlayer = FindObjectsOfType<MovePlayer>();
  27. loseScreen.SetActive(false);
  28. winScreen.SetActive(false);
  29. }
  30.  
  31. // Update is called once per frame
  32. void Update()
  33. {
  34. ballsLeftText.text = "Balls Left: " + ballsLeft;
  35. }
  36.  
  37. public void CatchBall()
  38. {
  39. ballsLeft -= 1;
  40. if(ballsLeft < 0)
  41. {
  42. ballsLeft = 0;
  43. }
  44.  
  45.  
  46. if (ballsLeft == 0)
  47. {
  48. Destroy(spawnerParent);
  49. Destroy(spawnerParent2);
  50. Destroy(spawnerParent3);
  51. DisplayWinScreen();
  52. movePlayer.enabled = false;
  53. }
  54. }
  55.  
  56. public void LoseLife()
  57. {
  58. livesLeft -= 1;
  59.  
  60. if(livesLeft == 4)
  61. {
  62. livesImg5.gameObject.SetActive(false);
  63. }
  64.  
  65. if (livesLeft == 3)
  66. {
  67. livesImg4.gameObject.SetActive(false);
  68. }
  69.  
  70. if (livesLeft == 2)
  71. {
  72. livesImg3.gameObject.SetActive(false);
  73. }
  74.  
  75. else if(livesLeft == 1)
  76. {
  77. livesImg2.gameObject.SetActive(false);
  78. }
  79.  
  80. else if(livesLeft == 0)
  81. {
  82. Destroy(livesImg);
  83. Destroy(spawnerParent);
  84. Destroy(spawnerParent2);
  85. Destroy(spawnerParent3);
  86. movePlayer.enabled = false;
  87. DisplayLoseScreen();
  88. }
  89.  
  90. else if(livesLeft <= 0)
  91. {
  92. livesLeft = 0;
  93. }
  94. }
  95.  
  96. public void AddLife()
  97. {
  98. Debug.Log(livesLeft);
  99. livesLeft += 1;
  100. Debug.Log(livesLeft);
  101. }
  102.  
  103.  
  104.  
  105. private void DisplayLoseScreen()
  106. {
  107. loseScreen.SetActive(true);
  108. }
  109.  
  110. private void DisplayWinScreen()
  111. {
  112. AudioSource.PlayClipAtPoint(winningSFX, Camera.main.transform.position);
  113. winScreen.SetActive(true);
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement