2nd animation is not working after 1st

Greetings,

Here is my code -

  • usingUnityEngine;
  • usingSystem.Collections;
  • publicclass enemyai :MonoBehaviour{
  • publicTransform target;
  • NavMeshAgent agent;
  • // Use this for initialization
  • voidStart(){
  • agent =GetComponent();
  • }
  • // Update is called once per frame
  • voidUpdate(){
  • agent.SetDestination(target.position);
  • GetComponent().Play(“Run”);
  • if(Input.GetKeyDown(KeyCode.LeftAlt))
  • {
  • GetComponent().Play(“Attack_01”);
  • }
  • }
  • voidOnTriggerEnter(Collider hel)
  • {
  • if(hel.gameObject.tag ==“enemy”)
  • {
  • GetComponent().Stop(“Run”);
  • }
  • }
  • }

It’s not playing 2nd animation cause it’s not stopping 1st , if i press left alt then it just play it for i millisecond , just a little bit and then again play run animation as loop .

can you please tell me how can i correct this in unity 5.1 , it have GetComponent so a bit confusing for me , can you write that code line to fix this , thanks .

I got the solution - here is the final code, hope it helps you -

using UnityEngine;
using System.Collections;

public class enemyai : MonoBehaviour {
    public Transform target;
        NavMeshAgent agent;
    public bool attack=false;
    // Use this for initialization
    void Start () {
        agent = GetComponent<NavMeshAgent>();

    }
   
    // Update is called once per frame
    void Update () {

        agent.SetDestination(target.position);
    if(attack==false)
        {
        GetComponent<Animation>().Play ("Run");
        }
        if(attack==true)
        {
            GetComponent<Animation>().Play("Attack_01");
        }
    }

    /*    GetComponent<Animation>().Play ("Run");

        if(Input.GetKey(KeyCode.LeftAlt))
           {

            GetComponent<Animation>().Play("Attack_01");
        }

    }*/
    void OnTriggerStay(Collider stay)
    {
        if(stay.gameObject.tag == "areap")
        {
   
                attack=true;

        }
   
    }
    void OnTriggerExit(Collider exit)
    {
        if(exit.gameObject.tag == "areap")
        {
            attack = false;
        }
    }


    }