using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; public class Attacker : MonoBehaviour { [SerializeField] float hurtDuration = 20f; [SerializeField] float damage = 1f; [SerializeField, Header("Register to know when I attack")] UnityEventFloatArg Attack; // Use this for initialization void Start () { StartCoroutine(HurtPlayer()); } // Update is called once per frame void Update () { } private void OnDestroy() { if (Attack != null) Attack.RemoveAllListeners(); } public void AddAttackListener(UnityAction action) { if (Attack == null) Attack = new UnityEventFloatArg(); Attack.AddListener(action); } IEnumerator HurtPlayer() { if (Attack != null) Attack.Invoke(damage); // Pause for a bit yield return new WaitForSeconds(hurtDuration); // Start hurting player again yield return StartCoroutine(HurtPlayer()); } }