Unity: How to relay messages sent by AnimationEvents

Here’s a reusable script for preloading AnimationClips with AnimationEvents: (in Unity 2.6 you can do this directly in the new Animation Editor, but in Unity iPhone this is a nice way to do it)


var targetClipName : String = "idle"; //Where should the Event be added? (the name of the AnimationClip)
var throwTime : float = 1.3; //When should the function be called?
var throwFunctionName : String = "MyFunction"; //What's the name of the function?

function Start () {

var throwEvent : AnimationEvent = new AnimationEvent();
throwEvent.time = throwTime;
throwEvent.functionName = throwFunctionName;



And since my scripts are never in the right place to catch messages sent by AnimationEvents, here’s a script to relay a message. If this script catches a message called RelayMessage it sends message to target. So just set the AnimationEvent to send a message named “RelayMessage” and then enter real target and real functionName here.

var target : GameObject;
var message : String = "FunctionName";

function RelayMessage () {



To sum it up: With the first scripts you can add an AnimationEvent to an AnimationClip and with the second script you can easily relay that message from the gameObject that holds the animation to some other gameObject that is the intended recipient.

Bookmark the permalink. Follow any comments here with the RSS feed for this post. Trackbacks are closed, but you can post a comment.

Post a Comment

Your email is never published nor shared. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>