Hello,
I have a few animations, that have animation events. its an external package that provides a script attached to the game object with the animator, and the function is called OnCustomEvent.
/// <summary>
/// Animation events. If you want to get animation callback, use it.
/// For example, if you want to know exact hit moment for attack animation, use custom event 'Hit' that is fired in most attack animations.
/// </summary>
public class AnimationEvents : MonoBehaviour
{
/// <summary>
/// Subscribe it to get animation callback.
/// </summary>
public event Action<string> OnCustomEvent = s => { };
I subscribe to the OnCustomEvent with the following code:
foreach(Collider2D enemy in hitEnemies)
{
Debug.Log("We hit " + enemy.name);
/*var damage = Random.Range(15, 30);
var enemyScript = enemy.GetComponent<Enemy>();
if (enemyScript)
{
TakeDamage(player, enemyScript, damage);
}*/
enemyHits.Add(enemy);
}
animationController.OnCustomEvent += OnHit;
}
public void OnHit(string text)
{
GameObject.Find("DebugLog").GetComponent<TextMeshProUGUI>().text = text;
foreach (var enemy in enemyHits)
{
var damage = Random.Range(15, 30);
var enemyScript = enemy.GetComponent<Enemy>();
if (enemyScript)
{
TakeDamage(player, enemyScript, damage);
}
}
enemyHits = new List<Collider2D>();
}
The problem is, the code works when I am in playmode, but when i build the game, the OnHit subscriber never seems to get called.