Hi all,
I’m basically asking for this question again:
… 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!