Animation Events problems

Hi guys. I have an error with Animation Events. I create an event on an animation clip, add a method and parameters. This works great.
If I do this via script, then the Unity throws an error, but the code still works fine and nothing breaks.

Error:
The function must have either 0 or 1 parameters and the parameter can only be: string, float, int, enum, Object and AnimationEvent.

public class EventForAnimator : MonoBehaviour
{
    [SerializeField] private List<Clip> clips;

    public void SubscribeAnimationEvents()
    {
        for(int i = 0; i < clips.Count; i++)
        {
            if(clips[i].AnimationEvents.IsNullOrEmpty()) continue;
        
            for(int j = 0; j < clips[i].AnimationEvents.Length; j++)
            {
                if(clips[i].AnimationEvents[j] == null) continue;
            
                var newAnimationEvent = new AnimationEvent();
                newAnimationEvent.time = clips[i].AnimationEvents[j].time;
                newAnimationEvent.objectReferenceParameter = clips[i].SimpleEvents[j];
                newAnimationEvent.functionName = "Invoke";
                AnimationUtility.SetAnimationEvents(clips[i].AnimationClip, new [] {newAnimationEvent});
            }
        }
    }

    public void Invoke(SimpleEventRoot simpleEvent)
    {
        simpleEvent.Invoke();
    }
}


The problem is resolved. The problem was that the called method was called “Invoke”.
There is to create any animation, add an event that calls the “Invoke” method, then there will always be an error

P.S. Unity 2019.2.0f1

I encountered this issue on 2019.1.14 too. Setting up the AnimationEvent manually to invoke a method named “Invoke” was enough to trigger it. The method was calledjust fine, but a few frames later errors were logged for that AnimationEvent and another one invoking a completely different method.
Renaming my method so that it wasn’t “Invoke” resolved the issue.

This is not the problem. You need to dig a little deeper to understand why it’s happening. Unity uses .net reflection feature to call your animation event functions by it’s names. You can declare function

void MyFunc()
{
}

and add event MyFunc and it will be called from animation via reflection. However, there are limitations. One of them is what functions you call from animation events can have one argument or no arguments. You can’t call function declared like this

void NotPossibleToCall(int num, string str)
{
}

from animation event. C# supports method overloads. You can declare multiple functions with different number and types of args.

void ClipEventHandler() {}
void ClipEventHandler(int num) {}
void ClipEventHandler(int num, string str) {}

When unity searches function to call from clip, it gets all overloads and iterates them to find the fittest one. In the example above, if you set up your event without args, first one will be called, and if you add int argument it will call the second one. But in both cases it will warn you there’s another ClipEventHandler which can not be called, it is the third one with two args.

This is what happens when you add Invoke event. Invoke method have multiple overloads with different args lists, including overloads with two args. That’s why you see this warning when setting Invoke as clip event. And that’s why everything works despite the warning.