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 /
  • Help Room /
avatar image
0
Question by FlammusLucis · Jun 09, 2017 at 12:21 AM · arrayinheritanceclasses

Array of unique inherited classes?

Hi, I am trying to make a game where the player can change between many unique attacks, so I was planning on making a base class "Attack" with a method "activate" for when it is called, then in my player combat script I would have an array of classes that inherit from Attack, each one with different code inside the activate method, but I can't figure out how to actually do it.

Here is an example of what I was doing:

 public class Player : TurnType {
 
     public List<AttackTypes> attacks = new List<AttackTypes>();
 
     private AttackTypes currentAttack;
 
 
     void Awake(){
         currentAttack = attacks [0];
     }
 
     public override bool Attack (){
         if (Input.GetButtonDown ("attack")) {
             base.Attack ();
             print (attacks);
             currentAttack.Activate ();
             return true;
         }else {
             return false;
         }
     }
 }
 
 
 [System.Serializable]
 public class AttackTypes : MonoBehaviour {
     void Awake(){
         GameGlobal.player.GetComponent<Player> ().attacks.Add (this);
     }
     public virtual void Activate(){
     }
 }
 
 [System.Serializable]
 public class fireball:AttackTypes {
 
     public override void Activate(){
         base.Activate ();
             // code for attacking
     }
 }



Comment
Add comment · Show 5
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 PizzaPie · Jun 09, 2017 at 06:15 PM 0
Share

So what is the issue? This should work, what i don't get is why you need to have the AttackType class inherit from $$anonymous$$onoBehaviour do you need to add it as a Component to a GameObject, or more precisely do you need the Awake Start etc functions on each attack type?

avatar image FlammusLucis · Jun 09, 2017 at 06:40 PM 0
Share

Thank you for the reply.

I don't know what the problem is, other than I know that the Awake() function is never called, so I think it might be something to do with the class never having an instance of it being made? I'm fairly new to program$$anonymous$$g and I only have the most basic understanding of the concepts.

I have it inherit from $$anonymous$$onoBehavior because I need the Start and Awake functions, it doesn't need to be attached to a gameobject.

avatar image PizzaPie FlammusLucis · Jun 09, 2017 at 06:58 PM 1
Share

As long they are not attached to any GameObject these functions won't get invoked thus either you keep them as $$anonymous$$onos and attach them to a GameObject else remove it and handle it internally. Ex.

 void Awake()
     {
         attacks.Add(new fireball());
         currentAttack = attacks[0];        
     }

And of course you need to call your Attack() method from somewhere which is not included on above scripts.

Note1: please use comments to reply ins$$anonymous$$d of answer.

avatar image FlammusLucis FlammusLucis · Jun 09, 2017 at 07:21 PM 0
Share

Thank you for your help, that works.

Sorry about the wrong format on the reply, I'm still trying to get used to the website.

avatar image PizzaPie FlammusLucis · Jun 09, 2017 at 07:52 PM 0
Share

No worries. Good luck with your project!

0 Replies

· Add your reply
  • Sort: 

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

115 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 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 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 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 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

(SOLVED) Accessing (string) arrays by enumerations from another class 0 Answers

Inheritance and Update Method 1 Answer

C# - creating a list of classes 5 Answers

Help with object oriented 0 Answers

Using a subclass in the inspector in place of a parent class and accessing the subclass variables 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