AnimationEvent.functionName not calling outside function when attack animation occurs.

I am new to Unity coding so please excuse my lack of terminology. The code here shows the player script for movement and attack for a generic click to move/attack diablo style game.

Full Code

  using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using System;
    
    public class Player : Creature 
    {
    	public static Transform opponent;
    
    	public static bool isAttacking;
    
    	public static Player player;
    
    	Animation animation;
    
    	public AnimationClip attackAnimation;
    
    	public float attackImpact;
    
    	// Use this for initialization
    	void Awake () 
    	{
    		player = this;
    		initAnimations();
    		isAttacking = false;
    	}
    	//Initialize animations
    	void initAnimations()
    	{
    		animation = GetComponent<Animation>();
    		AnimationEvent attackEvent = new AnimationEvent();
    		attackEvent.time = attackImpact;
    		attackEvent.functionName = "Timeimpact";
    		attackAnimation.AddEvent(attackEvent);
    	}
    
    	void Timeimpact()
    	{
    		Debug.Log("Attack at this time");
    	}

void initAnimations()
     {
         animation = GetComponent<Animation>();
         AnimationEvent attackEvent = new AnimationEvent();
         attackEvent.time = attackImpact;
         attackEvent.functionName = "Timeimpact";
         attackAnimation.AddEvent(attackEvent);
     }
 
     void Timeimpact()
     {
         Debug.Log("Attack at this time");
     }

Issue: I cannot get AnimationEvent.functionName to call the outside function TimeImpact().

For the following code below:

void initAnimations()
         {
             animation = GetComponent<Animation>();
             AnimationEvent attackEvent = new AnimationEvent();
             attackEvent.time = attackImpact;
             attackEvent.functionName = "Timeimpact";
             attackAnimation.AddEvent(attackEvent);
         }
     
         void Timeimpact()
         {
             Debug.Log("Attack at this time");
         }
  • A new animation event is declared
    (attackEvent).
  • attackEvent.time = attackImpact which
    is 0.37(set on Unity console). That
    means attackImpact = 0.37 or 0.37
    seconds in the attack animation.
  • attackEvent.functionName = “Timeimpact” which means a function
    called Timeimpact() should be called by attackEvent.functionName
  • Timeimpact() has a simple debug log
    mssg “Attack at this time”

The goal of the TimeImpact() is to generate the debug log which will state “Attack at this time” when the animation event at 0.37 seconds of the attack animation triggers.

When I run the code, and press play in Unity, all animations work and there aren’t any errors. The only issue that remains is that the debug log doesn’t output the line of text I specified in 0.37th second of the attack animation. Please let me know if I am doing something wrong with the code with specifics.

So I think the main area of focus in the code is the initAnimations and Timeimpact blocks. Awake just calls initAnimations and initAnimations calls Timeimpact.

Please let me know if you need the full code to assist.

Thanks

Just for a quick check, try triggering a few milliseconds before the actual animation length. It may end before it can trigger.

Maybe The script should be bound to the object of animation