Launch animation with a click on a button

Hi !
I’m trying to launch the animation of my creature when I click on a button.

In the script I created a void stop for my button but it doesn’t work… In my console the word “coucou” appears but it doesn’t play the animation and I don’t know what is wrong :confused:

using UnityEngine;
using System.Collections;

public class attackCreature : MonoBehaviour
{




    Animation anim;
    public void stop()
    {
        Debug.Log("coucou");
        anim["creature1Attack1"].wrapMode = WrapMode.Once;
        anim.Play("creature1Attack1");


    }



    void Start()
    {
        anim = GetComponent<Animation>();
        anim.Play("creature1Idle");
    }


    void Update()
    {
        var goblin = GameObject.Find("GOBLIN");
        var creature = GameObject.Find("creature1");
        var distance = Vector3.Distance(goblin.transform.position, creature.transform.position);
        //Debug.Log(distance);
        if (distance < 2)
        {
            
            anim.Play("creature1walkright");
        }
        if (anim.IsPlaying("creature1Attack1"))
        {
            
            anim.Play("creature1Idle");
        }
        if (distance >= 2)
        {
            
            anim.Play("creature1Idle");
        }


    }
}

If I see correctly you are playing the animation and instantly play its idle again in the Update.

That’s right, the character changes its animation for 1 second and then it plays idle again.
But how can I tell him to play idle only when the previous one is finished?

If(!anim.isPlaying("creatureattack") && !anim.isPlaying("creatureidle"))

This way the idle animation will only be played if creatureattack and idle aren’t playing.

Forgive me if syntax error or bad placement of the ‘!’ but I’m writing this reply right before heading to bad.

Any reason you are not using Meccanim? It certainly beats writing your own animation state machine in code.