GraveYardJokes

Hello,

I was wondering if I could get some help understanding how to subscribe to events. I currently am trying to subscribe to an Attack event in my character's attack animation. I set it up in Spine properly. I just am not getting a reading on the event when I subscribe in unity. Note: the animation start and end fire, just not the attack.

[SerializeField]
private SkeletonAnimation SkeletonAnimation;
[SpineEvent] public string attackEventName = "Attack";


protected override void Start()
{
SkeletonAnimation.AnimationState.Event += AnimationState_Event;

SkeletonAnimation.AnimationState.Start += AnimationState_Start;
SkeletonAnimation.AnimationState.End += AnimationState_End;
}

private void AnimationState_End(TrackEntry trackEntry)
{
Debug.Log("End");
}

private void AnimationState_Start(TrackEntry trackEntry)
{
Debug.Log("End");
}

private void AnimationState_Event(TrackEntry trackEntry, Spine.Event e)
{
if (e.Data.Name == attackEventName)
{
Debug.Log("Fired!");
}
}

---

Further investigation I found that I had the variable SkeletonAnimation type the same way as the type name. I now am getting a null reference on "SkeletonAnimation.AnimationState.Event += AnimationState_Event;". So now I know the object is not connecting with the Event. Any pointers on what to do here to display a Debug.Log("Attack Fired!") in the console when the event fires?
GraveYardJokes
  • Posts: 4

Misaki

Hmm, except for the problem that you had the variable SkeletonAnimation type the same way as the type name, it seems not to have a major problem. To make things clear, the following simple code should work:
using UnityEngine;
using Spine.Unity;

public class HandleEvent : MonoBehaviour
{
SkeletonAnimation skeletonAnimation;

[SpineEvent]
public string attackEventName;

void Start () {
skeletonAnimation = GetComponent<SkeletonAnimation>();
skeletonAnimation.AnimationState.Event += HandleAnimationStateEvent;
}

private void HandleAnimationStateEvent (Spine.TrackEntry trackEntry, Spine.Event e) {
if (e.Data.Name == attackEventName) {
Debug.Log("Fired!");
}
}
}
You seem to be writing the event name directly in the code, but I think it would be more accurate to set it from the pull-down menu that appears in Inspector, so please check to see if the correct event name is specified that way.
Screen Shot 2022-11-15 at 13.08.57.png


By the way, when you post your code, please enclose it in [ code][/code] tags so that it can be displayed as shown above. (An extra space is added in front of "code" so that it can be displayed here, but it is unnecessary for actual use.)
You do not have the required permissions to view the files attached to this post.
User avatar
Misaki

Misaki
  • Posts: 1163

GraveYardJokes

Thanks for the info. I'm not sure why but the code doesn't seem to be working for me. I fixed the type having the same name as the variable and now I'm not getting any errors, the event just doesn't fire. I'm going to try setting up a spine character with events in a new project and see if it works.

---

Update: Event IS firing in new project. Now Maybe Ill update the spine runtimes

---

Update: I fixed the Event not firing. Silly me did not update the Referenced animation when I remade my player pre-fab. So the event I was trying to fire was on a different copy of the animation that actually had the event on it. Silly me, but it's working well and now I can use Events how I want. Thanks for helping.
GraveYardJokes
  • Posts: 4

Misaki

I’m glad to hear you've figured it out! :)
User avatar
Misaki

Misaki
  • Posts: 1163


Return to Unity