Search Unity

AnimationEvent don't let change parameters at runtime

Discussion in 'Animation' started by HedgehogNSK, May 13, 2020.

  1. HedgehogNSK

    HedgehogNSK

    Joined:
    Jun 3, 2017
    Posts:
    24
    AnimationEvent has strange behaviour. When I'm trying to change parameter (int, object...) in foreach block it changes parameter value for current clipEvent but it doesn't change parameter value for animationClip.events! So I can suppose that IEnumerator returns reference to copy of the event which is instance of class. It looks like unexpected behaviour. Am I wrong?

    Code (CSharp):
    1.  
    2. namespace UnityEngine
    3. {
    4.     [RequiredByNativeCode]
    5.     public sealed class AnimationEvent
    6.     {
    7.        
    8.         public AnimationEvent();
    9. ...
    10.  
    11.         //     Int parameter that is stored in the event and will be sent to the function.
    12.         public int intParameter { get; set; }
    13.      
    14.         //     Object reference parameter that is stored in the event and will be sent to the
    15.         //     function.
    16.         public Object objectReferenceParameter { get; set; }
    17.      ...
    18.     }
    19. }
    20.  

    However I'm tried to change parameter through direct reference to event. As you can see setter of AnimationEvent is public. But parameter didn't change.

    Code (CSharp):
    1. public class ChangeAnimationEventParameter : MonoBehaviour
    2. {
    3.     [SerializeField] AnimationClip animationClip = null;
    4.     void Start()
    5.     {
    6.        
    7.         foreach (var clipEvent in animationClip.events.Where(@event => @event.functionName.Equals("OnEvent")))
    8.         {
    9.             //clipEvent.intParameter equals 0 at start
    10.             clipEvent.intParameter = 1;
    11.             Debug.Log(animationClip.events[0].intParameter + " "+clipEvent.intParameter);
    12.  
    13.         }
    14.  
    15.         animationClip.events[0].intParameter = 2;
    16.         Debug.Log(animationClip.events[0].intParameter);
    17.     }
    18. }
    19. //DEBUG LOG
    20. //0 1
    21. //0
    22.  
     
  2. gari090

    gari090

    Joined:
    Jun 14, 2020
    Posts:
    2
    Have you resolved this problem?
     
  3. reevthechameleon

    reevthechameleon

    Joined:
    Dec 22, 2020
    Posts:
    3
    I have some success by replacing the entire array after setting values in it:
    Code (CSharp):
    1. AnimationEvent[] ae = animationClip.events;
    2. ae[0].intParameter = 2;
    3. animationClip.events = ae;