- Home /
[Creating a generic death animation script] Child animations not showing
Hi all,
I'm basically asking for this question again:
http://answers.unity3d.com/questions/639749/how-do-i-access-the-animator-in-a-child-using-c.html
... With a difference. Using the editor is not an option, so none of the responses suit me.
I'll explain a bit further: I'm making a generic "death" GameObject which will be instanced upon the death of the player or an enemy when they die. It will then play a death animation, and destroy itself.
This is the code that destroys the current object and then creates the "death" GameObject in its place:
// When the player is killed...
private void OnDestroy() {
deathPlaceholder = Instantiate (deathPlaceholder);
deathPlaceholder.transform.position = gameObject.transform.position;
deathPlaceholder.AddComponent<DeathPlayer>();
}
The "DeathPlayer" component is, of course, the script which is attached to the player when he dies. This script is as follows, and I've made it so it's almost generic and can be reused with few changes:
using UnityEngine;
using System.Collections;
public class DeathPlayer : MonoBehaviour {
// ---------------------------------------------------------------------------------
// THESE TWO VARIABLES ARE THE ONLY THING YOU HAVE TO CHANGE FROM SCRIPT TO SCRIPT
// ---------------------------------------------------------------------------------
private const string ANIMATION_NAME = "Death_Player";
private const float ANIMATION_LENGTH = 0.767f;
// ---------------------------------------------------------------------------------
//private Animator animator;
public Animator animator;
void Start() {
// Get animator
//gameObject.transform.GetChild (0).GetComponent<Animator> ().enabled = true;
animator = gameObject.transform.GetChild (0).GetComponent<Animator>();
// Play death animation
StartCoroutine ("AnimateDeath");
// Destroy the object
Destroy (gameObject);
}
IEnumerator AnimateDeath() {
animator.SetBool ("Reverse", false);
animator.SetFloat ("Speed", 1.0f);
animator.SetInteger ("Start", 0);
animator.PlayInFixedTime (ANIMATION_NAME);
yield return new WaitForSeconds(ANIMATION_LENGTH);
}
}
To clarify: The animator contains ALL the animations for ALL the deaths of ALL the entities in the game, so it should all be pretty clean and tidy: I'll just make a script for every death, change those two constants at the start for whatever I need, and that's it.
The problem? I've debugged it, and all that code runs without errors, the "death" GameObject is created, the "animator" variable is not null and contains a UnityEngine.Animator, but no animation ever shows. It's just invisible.
What am I missing? What am I doing wrong? The other thread hinted at the animator not being "enabled" at the start, but as you can see (commented on the code) I've also tried "enabling" the animator before even loading it up, to no avail.
It's so sad that whenever I try to do some generic and tidy code in Unity I get these huge problems. It's like it forces you to do dirty, non-reusable code, and use the editor again and again :( Adding the Animator through the editor is not an option, since the script is added to the "death" object at runtime (remember: I have 1 death object, and multiple death scripts - one per entity).
Thanks in advance, everyone!
Answer by Trickman · Aug 19, 2016 at 11:55 AM
I got it to work! It was the layer on the sprite. Damn those pesky layers! Remember to always check a sprite's layer if it's not showing, since it may be hinding behind something else!
You can use this script if you want, guys. Let's make Unity a more clean code-friendly engine!
Answer by theANMATOR2b · Aug 18, 2016 at 03:21 PM
line 9 - private const string ANIMATION_NAME = "Death_Player"; line 22 - StartCoroutine ("AnimateDeath");
I'm not a coder so I'm probably way off - shouldn't these match? Death_Player and AnimateDeath?
From your detailed explanation - seems this is where something isn't working correctly.
Thanks for the answer and for taking your time to check my code.
No, it's not that. "AnimateDeath" is the name of the coroutine (a function that, in this case, animates the character), while "Death_Player" is the name of the animation.
This is a generic script where, if you simply change the ANI$$anonymous$$ATION_NA$$anonymous$$E to refer to a specific animation and the ANI$$anonymous$$ATION_LENGTH to whatever the length of that animation is, you can include it in any model of any game and it will work, saving tons of time recoding again and again.
Feel free to use it in your projects (if we get to fix it, of course!).
Your answer
Follow this Question
Related Questions
Parent animation animating child makes child's own animation not work 0 Answers
2D Animation does not start 1 Answer
Animation rotations are not rotating in the right direction. 1 Answer
Animating Child Isues 0 Answers
Keyframes of animated Child gameobject not shown by parent gameobjects animator issue ? 0 Answers