Hello everyone! First time using events. I’m trying to use a Unity Action called OnTurretDamaged that deals with all the damage done when a collision happens. The three methods that I am trying to use as subscribers takes no arguments except one that updates a countdown timer shown on screen (SetMostrador) . Is there a way to overload an unity action so I can use the same action for every method? If not is there a workaround? Thanks in advance.
The publisher
using System;
using UnityEngine;
public class ITakeHitsEnemyTankWithEvents : MonoBehaviour, IBreakableWithEvents
{
public static event Action OnEngineDamaged;
//public static event Action<Imostrador> OnEngineDamaged;
public static event Action OnTurretDamaged;
public static event Action OnCannonDamaged;
public static event Action OnTankDestroyed;
EnemyTankMovement tankMovement;
EnemyTurretRotation enemyTurretRotation;
EnemyTankFire tankFire;
TankExplosion tankExplosion;
MostradorDeTiempo mostradorDeTiempo;
private void Start()
{
tankMovement = GetComponent<EnemyTankMovement>();
enemyTurretRotation = GetComponentInChildren<EnemyTurretRotation>();
tankFire = GetComponentInChildren<EnemyTankFire>();
tankExplosion = GetComponent<TankExplosion>();
mostradorDeTiempo = GetComponent<MostradorDeTiempo>();
}
public void ITakeHitWithEvents()
{
RandomDamage();
}
public void RandomDamage()
{
float randomNumber = UnityEngine.Random.Range(0f, 1f);
//if (randomNumber < 0.25f) // movement disabled
{
OnEngineDamaged?.Invoke();
//tankMovement.ChangeWorkingState();
mostradorDeTiempo.SetMostrador(tankMovement);
Debug.Log("enemy tank movement disabled");
tankExplosion.PlayBlast();
//}
//if (randomNumber >= 0.25f && randomNumber < 0.5f) // turret rotation disabled
//{
// enemyTurretRotation.ChangeWorkingState();
// mostradorDeTiempo.SetMostrador(enemyTurretRotation);
// Debug.Log("enemy tank rotation disabled");
// tankExplosion.PlayBlast();
//}
//if (randomNumber >= 0.5f && randomNumber < 0.75f) // cannon disabled
//{
// tankFire.ChangeWorkingState();
// mostradorDeTiempo.SetMostrador(tankFire);
// Debug.Log("enemy tank cannon disabled");
// tankExplosion.PlayBlast();
//}
//if (randomNumber >= 0.75f && randomNumber < 1.0f) // tank disabled
//{
// Debug.Log("enemy tank destroyed");
// tankExplosion.PlayBlast();
// Destroy(gameObject);
//}
}
}
void OnEnable() { EnemyTankColliderWithEvent.OnTankDamage += ITakeHitWithEvents; }
void OnDisable() { EnemyTankColliderWithEvent.OnTankDamage -= ITakeHitWithEvents; }
}
The subscriber:
using UnityEngine;
using TMPro;
public class MostradorDeTiempo : MonoBehaviour
{
[Header("referencias")]
[SerializeField] Camera camara;
[SerializeField] TextMeshProUGUI textoMostrador;
EnemyTankFire enemyTankFireScript;
EnemyTankCollider enemyTankCollider;
EnemyTankMovement enemyTankMovement;
Imostrador imostrador;
public void SetMostrador(Imostrador mostrador)
{
this.imostrador = mostrador;
}
void Awake()
{
camara = Camera.main;
enemyTankFireScript = GetComponent<EnemyTankFire>();
enemyTankCollider = GetComponent<EnemyTankCollider>();
enemyTankMovement = GetComponent<EnemyTankMovement>();
textoMostrador.enabled = false;
}
void Update()
{
ActualizarTextoConTiempo();
//Debug.Log("FixTIme: " + enemyTankFireScript.GetFixTime());
//PosicionarElTextoDebajoDelTanque();
if (imostrador != null)
{
if (imostrador.GetCounter() <= 0.0f)
{
textoMostrador.enabled = false;
}
else
{
textoMostrador.enabled = true;
}
}
}
void ActualizarTextoConTiempo()
{
if (imostrador != null)
{
textoMostrador.text = imostrador.GetCounter().ToString("0.0");
}
}
void PosicionarElTextoDebajoDelTanque()
{
Vector2 posicionDelTanque = enemyTankCollider.transform.position;
Vector2 posicionDelTanqueEnPantalla = camara.WorldToScreenPoint(posicionDelTanque);
textoMostrador.rectTransform.position = posicionDelTanqueEnPantalla;
}
//void OnEnable() { ITakeHitsEnemyTankWithEvents.OnEngineDamaged += SetMostrador; }
//void OnDisable() { ITakeHitsEnemyTankWithEvents.OnEngineDamaged -= SetMostrador; }
}