Search Unity

Animation Event Function

Discussion in 'Animation' started by loko08, Feb 9, 2017.

  1. loko08

    loko08

    Joined:
    Oct 6, 2013
    Posts:
    57
    We try to keep our project structure as clean as possible, so I was wondering, if I need to call a function from an animation using animation event, the script file needs to be in the same object that has the animator (so far this worked).

    Can I reference a script that's somewhere else? If so, how?

    For example, right now I got my GameSounds script, that has the function playfootstep on the same object that has the animator component, but I would love to be able to have the GameSounds script located into a separate GameSounds object that I could use somewhere else.

    Thanks in advance!
    JP
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,332
    Nope! Has to be the same object.

    I find that the easiest way to handle animation events is with a script that picks up the events, which relays them to the correct components.
     
  3. MitchStan

    MitchStan

    Joined:
    Feb 26, 2007
    Posts:
    568
    You can have an animation event call a function that sends a UnityEvent. Then any listeners on any other gameobject can respond to the event. Very loosely coupled.
     
    loko08 likes this.
  4. loko08

    loko08

    Joined:
    Oct 6, 2013
    Posts:
    57
    Hmmmm yeah, I guess that makes sense. Still. It's weird that you have to create a script attached to the object in order for it to work, instead of just referencing it, or receiving the event from the outside.
     
    NEVER-SETTLE likes this.