- Home /
Pass animations from Animation component to abstract class' contructor
The following code is not working. To make it work I need to set animation clips in derived class as static. But when I do, how can I initialize them? Every Enemy must have Animation component and will contain these 3 basic animations. Base class contains some methods that use GetComponent()
to play these animations, so I don't have to write new e.g. Death() method in every derived class as it does always the same thing. Derived class Warrior is attached to Warrior prefab, that has Animation component with these basic animations. But the question is: How can I deliver these animations from Animation component to Enemy's constructor?
Thank you for any advice in advance.
CODE:
[RequireComponent(typeof(Animation))]
public abstract class Enemy : MonoBehaviour {
private AnimationClip _idleAnimation;
private AnimationClip _runAnimation;
private AnimationClip _deathAnimation;
public Enemy(AnimationClip idle, AnimationClip run, AnimationClip death) {
_idleAnimation = idle;
_runAnimation = run;
_deathAnimation = death;
}
public Death() { GetComponent<Animation>().CrossFade(_deathAnimation.name); }
}
public class Warrior : Enemy {
public AnimationClip idle;
public AnimationClip death;
public AnimationClip run;
public AnimationClip twoHandedAttack;
public Warrior()
: base(idle, run, death) {
}
}
Answer by Magnomous · Jun 19, 2015 at 02:27 PM
I realised that I will not be able to do instances of Warrior as it's base class inherits from MonoBehaviour. Therefore I deleted constructors and replaced them with method that initializes variables. And now I can pass public AnimationClips there. Problem solved.
Your answer
![](https://koobas.hobune.stream/wayback/20220613201149im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Simple Question: Animations and Base Class 1 Answer
Multiple Cars not working 1 Answer
Animation disturbing circular rotation 1 Answer
Animation at the end of the level 1 Answer