Why does this animation not play?

My animation doesn’t play when this object is instantiated, I’m at a loss? Can you see anything wrong with this? I have one animation attached to the object.

public class killspritescript : MonoBehaviour {

    Animation anim;

    // Use this for initialization
    void Start () {
        anim = GetComponent<Animation>();
        Invoke("destroyme", 4);
    }
  
    // Update is called once per frame
    void Update () {
        if(!anim.isPlaying)
        {
            anim.Play();   // no animation is played?
        }
    }

    private void destroyme()
    {
        Destroy(gameObject);
    }
}

Not sure why it’s not playing, but I do see that you could just play it in Start().
Also, you can delay ‘Destroy’ if you’re doing nothing special , other than waiting some amount of time.
If you remove Invoke and just write 'Destroy(gameObject, 4); ’ it’s the same thing :slight_smile:

From the code you posted, you have put an animation clip directly on your Object, I do not think this is supported anymore, you need to use an Animation Controller :

Though that’s usually recommended (and what I use), I still see Animation in the docs; I think it’s still valid.

True, it is in the docs, I have just made a test scene and put an animation clip directly on the GO, used the OP code, and it does not work. I opened up the anim file and changed it to a legacy animation, and then the code does work, kinda, ( the animation goes into playing state, but the animation does not actually play in the editor)

Weird one for me, Will probably be sat here for 2 hours now trying to get it working lol

lol. Interesting. Hm. :slight_smile:

I never use those, so my experience is … nil :wink:

Well, all I have learned in my 5 mins is that I could use a lot of practice even with the world’s simplest animation (creation) lol.
However, I was able to create an animation that played.
Plus, after I did that, I noticed there is a play automatically button haha. So that bit of code to start it may not even matter.
I did have to change mine to legacy, also.
Not sure what else is wrong, sorry :slight_smile:

Thanks guys,

So solution is to use animation controller instead?

I do use animation controller for my player animation.

Got it working using Animator controller, very simple in the end!

Cool. Probably better that way. Glad you got it solved :slight_smile:

Thank you :slight_smile: Just need a cool generic ‘monster die’ animation.

Will be putting website up for the game soon, game is procedurally generated 2d game where you are falling down a very long hole (think downwell game) but a lot longer :slight_smile: Going to put puzzle elements into it too, it is looking good so far and runs on Android 60fps and PC/Linux. I’d port it to Mac/iPhone too if I had a mac!

I’ve attached two screen shots. Every time game is run, the scenery, aliens etc are all procedurally generated and it only uses 8 game objects for the massive levels :-), each game object is a massive mesh I generate.


Looks neat :slight_smile: I’ve never played a game like that, but it sounds cool. Best of luck with it all. :slight_smile:

Cheers :slight_smile:

It’s a good game for a quick play, not much thinking to do, just shoot away!

lol, cool :slight_smile:

I barely play games nowadays; I enjoy tinkering with Unity & helping others make games. But sounds fun. =)

I’ve wrote a lot of games going back to commodore 64! I’m a software development trainer now but want to still make games :slight_smile:

Nice :slight_smile:

Optimised the shooting and put some nice light animation in when bullet hits platforms. Just need have some breakable platforms in, thus if shoot platform which is part of mesh, make those vertices disappear…