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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to change the animation clip assigned to a Animator state via script ??

Discussion in 'Editor & General Support' started by huangyous, Feb 21, 2013.

  1. huangyous

    huangyous

    Joined:
    Jun 27, 2010
    Posts:
    55
    In my game there are hundreds of monsters,the attacking animations of them are different,But I don't want to write hundreds of Animator Controllers.So is there a way to use only one Animator Controller,and change the attacking animation clip assigned to the "Attack" state via script?
     
  2. UnLogick

    UnLogick

    Joined:
    Jun 11, 2011
    Posts:
    1,745
    That is currently not possible though it's something we want to add at a future date.
     
  3. AddisAbeba

    AddisAbeba

    Joined:
    Dec 23, 2013
    Posts:
    1
    16.07.2014
    Is the problem now solved? Thanks...

    "In my game there are hundreds of monsters,the attacking animations of them are different,But I don't want to write hundreds of Animator Controllers.So is there a way to use only one Animator Controller,and change the attacking animation clip assigned to the "Attack" state via script?"
     
  4. WikiMalik

    WikiMalik

    Joined:
    Nov 13, 2014
    Posts:
    16
    How About Now?
     
  5. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,984
    You can script the creation of animation controllers and attaching clips/building transitions to automate the process. This will at least cut out doing it manually.
     
  6. chariot

    chariot

    Joined:
    Jan 19, 2015
    Posts:
    1
    And as well, legacy animation doesn't work now. Well done Unity Developers.
     
    rahulk1991 likes this.
  7. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,984
    Are you sure? I may be wrong but I believe it still works in 5.2. Is it removed in 5.3?
     
  8. sara_conagher

    sara_conagher

    Joined:
    Jul 9, 2015
    Posts:
    8
    Last edited: Sep 15, 2016
    etzl likes this.
  9. rahulk1991

    rahulk1991

    Joined:
    Feb 22, 2019
    Posts:
    12
    How about now?
     
  10. Trend86

    Trend86

    Joined:
    Jul 25, 2019
    Posts:
    7
    How about now?
     
  11. Trend86

    Trend86

    Joined:
    Jul 25, 2019
    Posts:
    7
    A work around


    public AnimationClip[] Clips;
    Animation anim;

    void Start()
    {
    anim = GetComponent<Animation>();
    InvokeRepeating("ChangeAnimation", 0f, anim.clip.length);
    }
    int ClipNumber = 0;
    void ChangeAnimation()
    {
    ClipNumber++;
    if (ClipNumber >= anim.GetClipCount())
    {
    ClipNumber = 0;
    }
    anim.clip = Clips[ClipNumber];
    anim.Play();
    }