Can't select function in Animation Event

I just want to play a sound (would be nice if the Audio thing had a Play event), anyway I made a little script, added it to my object. I can see the script and all in the Animation editor ok. I add an Animation Event where I want the sound to play. Note I have no compiler errors.

So it brings up the 'Edit Animation Event' dialog, but there are no functions to select! The only 'choice' I have is (no function selected). My script looks a lot like the one in the manual's example. It's JS:

private var sound : AudioSource;

function Start()
{
    sound = GetComponent (AudioSource);
}

function Play ()
{
    sound.Play();
}

What am I doing wrong, or is this a bug?

I guess you maybe attached the script to the wrong place which should be attached to where the Animation component exists.

This appears to be a bug. Re-opening the scene solved this issue for me.

I tested what you describe and it worked perfectly fine. I cannot guess at what you have done wrong, but something about your description of your setup is missing. Maybe try a fresh scene and test what exactly you’re doing. I created a cube, added a sound and attached the script “function Play(){audio.Play();}” I created an animation in the Animation window that went from position x = 0 at frame 0 to position x = 200 at frame 40. I added an AnimationEvent at frame 20 and I was able to set the function for the Event in the dialog to Play().

If there is an error in the Script file that holds the functions you are trying to call they wont show up.

Play your scene and if it fails to run because of errors, fix them. After that the functions will hopefully appear in the AnimatorEvent drop-down.

If your scene plays fine you probably haven’t attached the script file to the GameObject you are animating.

try changing private var: AudioSource; to var: AudioSource; only… :slight_smile:

I think there is a bug in Unity where you cannot select any function if the gameObject has children. You have to delete all its children or move them select a method for an animation event and then move back the gameObject children. That’s how I fixed my problem.

From your post, this probably doesn’t pertain to you. But, this is why it wasn’t showing for me:

Check out this answer: Why don't animation events allow you to call functions that take a bool parameter? - Unity Answers

If you have a boolean parameter, it doesn’t show

public void

Problem solved