- Home /
How can i use AnimationEvent and SendMessageOptions.DontRequireReceiver ?
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class SwitchAnimations : MonoBehaviour
{
private Animator animator;
private int index = 0;
private static UnityEditor.Animations.AnimatorController controller;
private UnityEditor.Animations.AnimatorState[] an;
// Use this for initialization
void Start()
{
animator = GetComponent<Animator>();
an = GetStateNames(animator);
AnimationEvent ae = new AnimationEvent();
ae.messageOptions = SendMessageOptions.DontRequireReceiver;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
animator.Play(an[index].name);
if (++index == an.Length)
index = 0;
}
}
private static UnityEditor.Animations.AnimatorState[] GetStateNames(Animator animator)
{
controller = animator ? animator.runtimeAnimatorController as UnityEditor.Animations.AnimatorController : null;
return controller == null ? null : controller.layers.SelectMany(l => l.stateMachine.states).Select(s => s.state).ToArray();
}
}
I tried to add this two lines:
AnimationEvent ae = new AnimationEvent();
ae.messageOptions = SendMessageOptions.DontRequireReceiver;
But still after pressing A few times i'm getting this 3 exceptions:
'Space_Soldier_A_LOD1' AnimationEvent 'RollSound' has no receiver! Are you missing a component?
'Space_Soldier_A_LOD1' AnimationEvent 'CantRotate' has no receiver! Are you missing a component?
'Space_Soldier_A_LOD1' AnimationEvent 'EndRoll' has no receiver! Are you missing a component?
And when in the Hierarchy on the Space_Soldier_A_LOD1 i make Window > Animation i can't change/edit the animations and events i see a message: Please select a gameobject that does not have 'Optimize Game Objects' applied. I didn't understand what to do.
So then i tried to add this two lines to the script with the AnimationEvent but it didn't solve it. How can i solve this 3 exceptions ?
Answer by haimmoshe · Aug 29, 2017 at 11:37 AM
The solution is just to add the exceptions as empty methods in the script. since the animations have events they need and looking for the methods. The methods can be empty or you can use them later for example inside RollSound you can play some audio clip or just leave it empty but the method must be in the script.
It's much easier this way then messing up with the animations and events.
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.Animations;
using UnityEngine;
public class SwitchAnimations : MonoBehaviour
{
private Animator animator;
private static UnityEditor.Animations.AnimatorController controller;
private AnimatorState[] states;
// Use this for initialization
void Start()
{
animator = GetComponent<Animator>();
states = GetStateNames(animator);
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
StartCoroutine(QueueAnim(states));
}
}
private static UnityEditor.Animations.AnimatorState[] GetStateNames(Animator animator)
{
controller = animator ? animator.runtimeAnimatorController as UnityEditor.Animations.AnimatorController : null;
return controller == null ? null : controller.layers.SelectMany(l => l.stateMachine.states).Select(s => s.state).ToArray();
}
IEnumerator QueueAnim(params AnimatorState[] anim)
{
int index = 0;
while (index < anim.Length)
{
if (index == anim.Length)
index = 0;
animator.Play(anim[index].name);
AnimatorStateInfo si = animator.GetCurrentAnimatorStateInfo(index);
yield return new WaitForSeconds(5);
index++;
}
}
private void RollSound()
{
}
private void CantRotate()
{
}
private void EndRoll()
{
}
private void EndPickup()
{
}
}