0
\$\begingroup\$

In Legacy Animation Component of Unity Engine, I want to check if a specific Animation Clip exists in Animation Componenet, or not.

            animationComponenet.Play("clipName");

I have same script attached to different GameObjects. So, there is only a difference of 1,2,3 as ending of the clip name. For example, GameObject1 has "clipname1", GameObject2 has "clipname2" as Clips. ANd there are a number of GameObjects like this. But, I want the same script to play different clips. So, what I want to do is to call this method "animationComponent.Play(clipname):".

But since, there is different clips attached to different GameObjects so I want to check before calling Play method.

Like, some way to know if the Animation Componenet contains "clipname1" or "clipname2". I tried currently "try, catch" way.

    try
    {
        anim.Play(clip1Name);


    }
    catch 
    {
        try
        {
            anim.Play(clip1Name+"1");


        }
        catch 
        {
            try
            {
                anim.Play(clip1Name + "2");


            }
            catch 
            {
                anim.Play(clip1Name + "3");


            }
        }
    }

In order to avoid any Null Reference Exception as error should lead to catch which should succesfully run any animation. But, try/catch doesn't work. As it is still throwing Null Reference Exception which stops execution if an animation clip is not found.

What would be the most suitable way that it tries different names but still doesn't throw any Null Reference Exception.

\$\endgroup\$
  • 1
    \$\begingroup\$ When you play an animation clip with animation.Play(name) and no animation with that name exists, then it doesn't throw an exception. It returns false and that's it. Are you 100% sure the NullPointerException comes from that piece of code? \$\endgroup\$ – Philipp Jun 1 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.