How to detect animation event triggering error during runtime?

Windows 10 version 21H2 (OS Build 19044.1889)
Unity 2021.3.8f1
Google Cardboard XR Plugin for Unity version 1.16.0
XR Plugin Management Version 4.2.0 - October 21, 2021
Android 8, 10, 12
iOS 12.5.5

Hi,

How to capture error of an animation event triggering when its component is not assigned from Unity Editor based on instructions below ?

public class Foo : MonoBehaviour
{
    // The _fader component below is not yet assigned in 
    // the Unity Editor and scene played. 
    public Animator _fader; 

    public void OnFadeOut()
    {
        // Triggering animation event, how to trap this instruction in case error ?
        _fader.SetBool( "FadeOut", true );

        // This line will not be executed if _fader component is not assigned.
        Debug.Log( "I am here..." );
    }
}

Any idea ?

If you want to catch the exception, use exception handling

public void OnFadeOut()
{
    try
    {
        _fader.SetBool("FadeOut", true);
    }
    catch (Exception ex)
    {
        Debug.Log($"An error occured: {ex.Message}");
    }
}
1 Like

Privacy & Terms