Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Trickman · Aug 18, 2016 at 02:07 PM · animationanimatorchild object

[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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Trickman · Aug 19, 2016 at 09:51 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges