- Home /
Is it possible to send AnimationEvents to other gameObjects?
My characters have a setup where I use an empty GameObject as a parent, with the actual model and animations as a child. The scripts controlling the character are applied to the parent, so that I can easily replace the visuals without having to recreate the entire setup.
However this gives me a problem when using animations with animation events. They seem to default to send messages to the object containing the animation component, and I can't seem to find a way to send them to the parent object which actually contains the functions being called. Is there a smooth way to do this? Or do I have to add all the functions to a script on the object with the animations, that in turn pass them on to the parent object.
Answer by burtonposey · Feb 02, 2016 at 05:36 AM
I know this is a really old question, but since it turned up at the top of the search results when I was reapproaching this same problem (and forgot my own solution for it from a few years back), I'd figure I'd chime in.
What I do is I have a class called TargetedMessage. This is a direct relationship, so I know what GameObject I'm going to be talking to. For more open ended relationships for Events, you'll need a more robust solution. But for a direct, "I know who I need to talk to when this Animation is done", you can use something like this:
using UnityEngine;
using System.Collections;
public class TargetedMessage : MonoBehaviour
{
public GameObject target;
public string message;
public void DoTargetedMessage()
{
if (target != null)
{
target.SendMessage(message, SendMessageOptions.RequireReceiver);
}
}
}
On your GameObject with the Animation on it, you can attach this script. Hook up the target, and put a string literal of the method you wish to call. Make an AnimationEvent and set the Function to DoTargetedMessage. It'll send the method call directly to your target!
I am currently using this to target my main scene transition class so when I transition out of the scene, it's dependent on a camera animation finishing it's movement. At the end of that movement, I called this method to let the scene class that we're totally done and we can load the next scene.
Your answer
Follow this Question
Related Questions
I am trying to send a 2D texture to my first person controller to display on the GUI 1 Answer
SendMessage Not Working: JavaScript 1 Answer
Raycast check? 0 Answers
App in background still receive UnityPlayer.UnitySendMessage from Android plugin, normal ? 0 Answers
Send Message not working (editor?) 2 Answers