Search Unity

Question Help swapping animation for different game objects via code

Discussion in 'Animation' started by PaperMouseGames, May 8, 2020.

  1. PaperMouseGames

    PaperMouseGames

    Joined:
    Jul 31, 2018
    Posts:
    434
    Hi there! I'm working on a 2D tile based RPG and I'm fairly new to the animation part of things and I'm struggling with how to switch animations on a game object based on what other properties that game object has.

    Here are some more details about my project (using NPCs as an example):

    • I'm using game objects in the scene that have the following components: ActiveNPC, Sprite Renderer.
    • The ActiveNPC component holds a scriptable object called NPCData. This SO is where all the info about the NPC is held, like name, dialogue, and the sprite.
    • Upon waking up the ActiveNPC component reads in that NPCData and sets the sprite in the sprite renderer to the sprite in the SO.
    All of that works well, so then I can swap out the NPCData for an ActiveNPC in the scene and everything changes over.

    The issue is that when I add an Animator component to my NPC game objects, I can't figure out how to swap out the animations for each NPC, only the sprite.

    I can't get the SO to hold an Animator Controller, and I can't figure out how else to do this, but I'm sure there must be a way to swap out the animations of a given game object in the scene.

    Maybe I'm doing something wrong, but I had an Animator Controller for each NPC and then there were different states with the different Animations.

    Any help with this would be really appreciated!