Outdated scripting code about animation

Hi guys, I am recently do a seminar about Unity3D. I was excited about it when i realized that it’s a total crap one. I should have paid for the official Unity Certification. Anyway, what i am doing is to follow the instructor’s notices where i figure out that everything is outdated. At the moment I do the part of the animation, where with coding, you can control which animation you can play. So in his notices, he is using the command:

if (!animation.isPlaying)

which it was wrong obviously, because after some digging, I found out that this line is outdated. I replace it with the

if (!GetComponent())

My issue now that inside the if, there is another if which uses the wrong command to play the animation. So I am trying to figure out what should I change to make my code working. Below there is my code which needs the changes:

private void EnemyMovement()
{
if (!GetComponent())
{
SecondsToLaunch += Time.deltaTime;
if (SecondsToLaunch > LaunchCD)
{
animation.Play();
}
}
else
{

}
}

Before someone pops up and tell me “why you dont ask your assigned tutor about your issue”, I did and he told me “yeah, you are right, the command line is outdated, use the one that you mentioned” without telling me how to correct the whole part.
Any help will be appreciated.

update

I changed the line into
GetComponent().Play()

It seems its ok without errors, but is it the right way? and how it could have been if i wanted to play a specific animation clip?

This provides a bit of info: Unity - Scripting API: Animation.Play
:slight_smile:
Yes, your method was correct, and that link shows an option for playing a specific animation (if not the default, such as was in your case).

1 Like

thank you :slight_smile:

No problem. You’re welcome :slight_smile: