- Home /
Animations on instantiated prefabs not working
I'm trying to make a game where you equip weapons from a hotbar. When a weapon is selected from the hotbar, a prefab for the weapon with a script and an animator is spawned via Instantiate() in the player's "hands". All weapon scripts use an interface that allows the player's script to call methods on them.
The interface methods work correctly, but animations within these methods cause warnings. I'm trying to get a sword to play a swinging animation when the player equips it and left clicks. When equipping it, the sword's draw animation plays correctly, as the state machine would make it do. However, when left clicking, (The "PrimaryDown" method in the sword script) I get a "Animator does not have an AnimatorController" warning, even though the sword has both of those. The AnimatorController state machine has even been configured already.
When placing the sword prefab in the scene and calling PrimaryDown() on it, the animation plays normally. The animation also plays normally when it is told to play from Awake(), even if I instantiate the sword. Why doesn't the animation work when I instantiate the Sword at runtime?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Sword : MonoBehaviour, IWeaponFunctions {
Animator myAnim;
void Awake() {
myAnim = GetComponent<Animator>();
myAnim.Play("Swing"); //Plays if called from here
}
public void PrimaryDown() { //Implementation from IWeaponFunctions interface
myAnim.Play("Swing"); //But not from here
}
}
I can provide more details as needed, I'm not sure what would be causing this.
Hi, It's an old question but I ran into what I think is the same problem: this is a misleading warning message that occurs when using "Animator.Play" on an animator that is disabled (either the GameObject itself, or one of it's parent), or maybe on one that that has never been enabled since instantiated (occurred on 2017.1.0)
Using myobject.gameObject.SetActive(true) (or on the disabled parent) before calling the first "Play" should remove the message.