Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Magnomous · Jun 15, 2015 at 12:46 PM · c#animationabstract

Simple Question: Animations and Base Class

Hello. Just a simple question: Is it possible to work with animations inside of abstract class?

Lets say I have:

 public abstract class Enemy {
 
     private AnimationClip _attackAnimation;
 
     public virtual void Attack() {
 
         // and now I want do do something like this:
         GetComponent<Animation>().CrossFade(_attackAnimation.name);
     }
 }

Thanks for any help.

Comment
Add comment · Show 2
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 saud_ahmed020 · Jun 15, 2015 at 02:32 PM 0
Share

Why you are making abstract class? As we know we can not make object/instantiate abstract class. If you want to call this method you must override it in its sub-class/child class.

avatar image Magnomous · Jun 15, 2015 at 08:37 PM 0
Share

Thats right. I don't want to instantiate enemy. I want to have orcs, spiders, skeletons etc. that will use enemy's methods. But all of these enemies will have animation called "attack1". And thats why i am asking whether there is a way to call this animation once - in the Attack() method inside of enemy class - so i dont have to override it everytime.

1 Reply

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

Answer by Baste · Jun 15, 2015 at 01:27 PM

Yes

You need to subclass your Enemy class to actually do anything with it, but this is exactly how you implement abstract classes.

Though, for that to work, your Enemy class has to inherit from MonoBehaviour, otherwise GetComponent doesn't exist.

Comment
Add comment · Show 5 · 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 Magnomous · Jun 15, 2015 at 01:33 PM 0
Share

So, every class that is derived from this base class must have it's own implementation, even though it will always be the same implementation? $$anonymous$$g. if this Attack() function will have in 90% cases the same body (just play Attack animation, nothing more), then there is still no way to write it once and override only if needed?

avatar image Baste · Jun 15, 2015 at 01:42 PM 0
Share

No, you use the implementation from the base class unless you overwrite it. If you have the code above, and this class:

 public class SomeEnemy : Enemy { //NOTHING HERE }

that'll work.

avatar image Magnomous · Jun 15, 2015 at 08:32 PM 0
Share

Yes, I know how this works. But my question is whether there is a way to make method inside of abstract class that can play animation. Or somehow call animation component and tell it which animation should be played.

avatar image Baste · Jun 15, 2015 at 09:34 PM 0
Share

Yes, by having the abstract class inherit from $$anonymous$$onoBehaviour.

$$anonymous$$onoBehaviour is an abstract class, and abstract classes can inherit from other abstract classes.

avatar image Magnomous · Jun 16, 2015 at 06:13 AM 0
Share

Thanks, this was exactly what I needed to know.

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

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Pass animations from Animation component to abstract class' contructor 1 Answer

Help with character movement and camera 1 Answer

[C#] Unity 2D: Changing animation when player is in motion. 1 Answer


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