Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Can i control animation of object from different script ?

Discussion in 'Scripting' started by NjordNystrom, May 3, 2020.

  1. NjordNystrom

    NjordNystrom

    Joined:
    Feb 29, 2016
    Posts:
    25
    Hello friends, I've gravity gun and i pull objects and throw em to enemies. When it collide with enemies, they die and my objects which i called them ammo bouncing. I want to add die animation enemies but my collision script at my throwaway objects(ammo). How can add die animation for enemy objects.


    I added selected bool because these ammo objects on ground and enemies can touch when they walking.


    Code (CSharp):
    1.  public bool selected = false;
    2.  
    3.  
    4.     void OnCollisionEnter(Collision collision)
    5.     {
    6.         if (selected && collision.transform.tag == "Enemy")
    7.         {
    8.             Waypoint.Instance.DestroyEnemy(collision.gameObject);
    9.             Destroy(collision.gameObject);
    10.  
    11.  
    12.         }
    13.         //Debug.Log(collision.transform.name);
    14.     }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Do your enemies have some kind of Enemy script on them? I would suggest adding a Die() method to your enemy script and let your Enemy script handle the dying part for itself. Then in this collision code you can just call collision.gameObject.GetComponent<Enemy>.Die();
     
    NjordNystrom likes this.
  3. NjordNystrom

    NjordNystrom

    Joined:
    Feb 29, 2016
    Posts:
    25
    So, i add Die() function and apply play death anim and destroy objects elements in it. Then reach it from ammo script. I'will try thanks.
     
    PraetorBlue likes this.