Advertisement
Tarodev

[SerializeField] 2

Jan 29th, 2020
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.49 KB | None | 0 0
  1. public class Hero : MonoBehaviour {
  2.     [SerializeField]
  3.     private float _health; // This will still be available in the inspector
  4.  
  5.     public void TakeDamage(float dmg) {
  6.         _health -= dmg;
  7.         if(_health <= 0) Die();
  8.     }
  9.  
  10.     private void Die() {
  11.         // Make gurgling noises and die dramatically
  12.     }
  13. }
  14.  
  15. public class Enemy : MonoBehaviour {
  16.     private Hero _hero;
  17.  
  18.     void Start() {
  19.         _hero = FindObjectOfType<Hero>();
  20.  
  21.         _hero.TakeDamage(2);
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement