- Home /
Animation Events question: calling a function on a child object.
Hi, Im trying to setup an animation event so that when my character is halfway through the attack animation he will call the "attackEnemy" function.
This should be fairly straight forward but Im running into a little problem: The "attackEnemy" function is not on a script that's on the character itself, it's on and empty Object which is a child of this character, therefore I cannot access it from the Animation Editor.
Any ideas on how I can get around this?
If I manage to get this working I'll be able to get around a ton of hard coded values which will drastically improve my project.
Answer by Womrwood · Dec 02, 2013 at 06:08 PM
Use the function: http://docs.unity3d.com/Documentation/ScriptReference/Component.GetComponentsInChildren.html Where the generic parameter T is your Script class type and then call the function.
Or you could loop through all children calling SendMessage.
I prefer that my parent object instantiate all its children and save a reference to them so I can call the function directly instead of using the methods mentioned above which relies on reflection.
Thank you, I ended up using an alternate message which was simply creating a new function on my parent object which calls the "attackEnemy" function on the child, but I believe your method would have worked too.
Your answer
Follow this Question
Related Questions
Animator Events 0 Answers
Setting up EventTrigger programmatically for GameObject throws NullReferenceException 0 Answers
Animation Editor is Extremely Slow 0 Answers
Need Help With Animations! In C# 2 Answers