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 Gumiibear · Nov 04, 2016 at 12:18 PM · methodabstractdependency

Access subclass through base

I'm trying to access a method within a gameobjects script without knowing the scripts name. What I do know is that the scripts base is an abstract class "entityScript" which it inherents a few things like health and damagedealt.

With the following code I am able to get the first Vector3 out of an array "MovePattern" that has been assigned by my "unknown" script

 GameObject mover = movers [pos];
 var scriptEnemy = mover.GetComponent<entityScript> ();
 targetpositions [pos] = mover.transform.position + 32f * scriptEnemy.MovePattern[0];


The abstract base class

 public abstract class entityScript : MonoBehaviour {
 
     public int health;
     public int damageDealt;
     public Vector3[] Movepattern = new Vector3[5];
     }   }

And one example of the subclass that I want to find and use methods from without knowing the name.

 public class simpleEnemy : entityScript {
 
     // Use this for initialization
     void Start () {
         health = 10;
         damageDealt = 1;
         //Movepattern = new Vector3[]{Vector3.down, Vector3.down, Vector3.left, Vector3.right, Vector3.up};
     }
     
     public Vector3[] getMovePattern() {
         Movepattern = new Vector3[]{Vector3.down, Vector3.down, Vector3.left, Vector3.right, Vector3.up};
         return Movepattern;
     }}

Note that in this scenario the method I want to access is "getMovePattern" which will be the same name in every other subclass, but with different contents. So to summarize in once question, how do I access the methods in subclasses through the base class?

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

1 Reply

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

Answer by Zodiarc · Nov 04, 2016 at 12:19 PM

What you are trying to do is impossible by design and is against oop-programming rules. If you need to access a method from a subclas in its superclass that means that your overall design is flawed. Try the composition over inheritance principle https://en.wikipedia.org/wiki/Composition_over_inheritance

Or in your special case you could use the Template Method Pattern.

Short example:

 public abstract class Entity : MonoBehaviour {
     public Vector3[] getMovePattern() {
         return this.getSpecialMovePattern();
     }
 
     protected abstract Vector3[] getSpecialMovePattern();
 }
 
 public class Enemy : Entity {
     protected override Vector3[] getSpecialMovePattern() {
         return new Vector3[]{Vector3.up, Vector3.down};
     }
 }


Comment
Add comment · Show 2 · 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 Gumiibear · Nov 04, 2016 at 12:40 PM 0
Share

Thank you, I used your second alternative and it works just as I wanted it to now.

avatar image Owen-Reynolds · Nov 04, 2016 at 02:42 PM 0
Share

It may be against OOP rules (that's subjective,) but it's common to check for a subclass: Enemy e1 = someEntity as Enemy; if(e1!=null) .. .

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

Error CS1061...does not contain a definition for...and no extension method 1 Answer

When the Application.LoadLevel() called, is the remaining code works? 0 Answers

Technically Match 3 but not really 0 Answers

AdControl and Unity C# Projects 0 Answers

What go inside the brackets in voids? 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