- Home /
Change Animator Parameters on Different Object - Prefabs
Hi Everyone,
So I have a classic 2D RPG Battle System I’m working on, just like every Final Fantasy from 1 - 6. Each character is a Prefab, with an Animator attached. I’ve setup a Blend Tree for Physical Attack, Magical Attack, Hit, and the default state Idle.
Battle scene loads, character animates in the default Idle state. Awesome, good so far. I decided then to try out setTrigger parameters for my transitions from Idle to Physical Attack, and made a very simple script that I’ve also attached to the character prefab that triggers these transitions using Keycodes U and I, so during the battle I can hit say U, and it triggers the Physical Attack animation, and then I can hit I, and back to Idle again. Cool.
Now I want to change my setTrigger parameters in my main battle script, and I’m seriously losing my mind. In the main battle script, I set public GameObject and Animator variables, linked them in the inspector and simply test this out using GetCompenent and changing the setTrigger to exactly what put on the main player objects script, and all I get in the console is “Animator is not playing an Animator Controller”.
So TLDR, I have a player prefab with an animator, I want to control this animator from a different prefab and script and all methods of GetComponent I’ve found online including the docs just will not work from a different game object. I’m about to post the code, but hopefully someone can start thinking for me. I imagine this is silly easy and I’m missing something. Thanks in advance, code to follow.
Following is attached to character prefab and works as expected to change animation
public class AttackAnimation : MonoBehaviour
{
public Animator thePlayer;
Animator m_Animator;
public void Start()
{
//Get the Animator attached to the GameObject you are intending to animate.
m_Animator = gameObject.GetComponent<Animator>();
}
void Update()
{
//Press U - goes from Idle to Physical Hit within Animator
if (Input.GetKey(KeyCode.U))
{
//Reset the "Idle" trigger
m_Animator.ResetTrigger("Idle");
//Send the message to the Animator to activate the trigger parameter named "Hit"
m_Animator.SetTrigger("Hit");
}
//Press I -goes from Physical Hit to Idle within Animator
if (Input.GetKey(KeyCode.I))
{
//Reset the "Jump" trigger
m_Animator.ResetTrigger("Hit");
//Send the message to the Animator to activate the trigger parameter named "Idle"
m_Animator.SetTrigger("Idle");
}
}
}
This snippet is from beginning of my Battle Script (this is also a gameObject/Prefab) - the two variables are linked within inspector (I do think its weirds however that the Animator needs me to drop the character prefab on it and won't accept just the animator controller... also in the drop down list, nothing shows up... anyway in Awake I believe I'm grabbing the Animator from my other character prefab I've linked up right?)
public Animator attackAnim;
public GameObject theCharacter;
// Use this for initialization
void Awake()
{
attackAnim = theCharacter.GetComponent<Animator>();
}
Then finally to make this really simple, during the player attack I am just using setTrigger like this - but I've tried Play to - and all I get is Animator is not playing an Animator Controller warnings)
attackAnim.SetTrigger("Hit");
This post is already half a mile long, but trust I'm stuck. If you need to see anything else please ask, and know I appreciate you all and what you do.
Answer by olafmayer81 · Nov 05, 2020 at 09:53 AM
Hi. did you ever find an answer to your problem? I am having exactly the same issue. I think it might have to do with linking a prefab to the public animator instead of the instance that is actually on stage...but I am still struggling to make it work on my game tbh.
Your answer
Follow this Question
Related Questions
Error with my strafe animations on player 0 Answers
how to make multiple transition from one state? 1 Answer
Blending animations for a bow with differing charges 1 Answer
Animation controller attached to prefab instance doesn't work 0 Answers
2DAnimator Freezes first time an animation is played 0 Answers