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. Dismiss Notice

Change 2D-sprite of an gameobject that has animator attached

Discussion in 'Animation' started by DanWef, Nov 18, 2020.

  1. DanWef

    DanWef

    Joined:
    Jun 4, 2020
    Posts:
    27
    Hello! I would like to change the sprites of an gameobject while runtime. The problem is, the gameobject has a animator with some animationen states attached to it, so I cant change the sprite with GetComponent<SpriteRenderer>.sprite. I want to change the sprites of the gameobject randomly when its animator is in a state where no animation clip is attached to. Do you have any ideas? Thx!
     
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,486
    You need to set it in LateUpdate which runs after the animation update.
     
  3. DanWef

    DanWef

    Joined:
    Jun 4, 2020
    Posts:
    27
    Thx, that works but unfortuently the my algorithm that changes the sprite runs to fast on LastUpdate. I dont know why. When it runs from animation update (I tried the algorithm with an gameobject that has no animator attached to it) it is running good.
     
  4. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,486
    Run your algorithm whenever you want, store the Sprite, then assign it in LateUpdate.