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
1
Question by DonPerry · Mar 28, 2016 at 06:29 PM · c#inheritancedictionary

Overriding Start(), is this just bad code?

I'm currently trying to understand inheritance with regards to a base class that derives from MonoBehaviour.

This one's kind of a two part-er. First, the Dictionary 'actions' that's created in the base class only fires off the most recently added method despite the fact all options seem to be displaying just fine; it lists them all correctly in the game. I'm assuming that this is due to the way I'm setting up the inheritance with these objects.

So I'm starting to observe some undesired behavior from this design pattern and I suspect that this type of inheritance is just a bad solution to the whole system. Particularly:

  • It's rather verbose when it comes to descendant Start() methods

  • My Dictionary<string, Action> is acting very strange.

I've declared:

  • GamePiece : MonoBehaviour

  • Structure : GamePiece

  • Generator : Structure

Currently I'm overriding GamePiece.Start() and calling base.Start() on every public override Start() for my each descendant.

So rather than directly ask a question about why the Dictionary isn't working, I figured I'd start with the design itself. Is this just bad design and if so, is there a better way to approach inheriting from a MonoBehaviour?

GamePiece :

     public class GamePiece : MonoBehaviour {
         public Dictionary<string, Action> actions;
         public virtual void Start () {
               actions = new Dictionary<string, Action>();
               actions.Add("Destroy", () => Destroy(this.gameObject));
         }
 }

Structure :

 public class Structure : GamePiece {
 
     public override void Start() {
         base.Start();
     }
 }

Generator:

 public class Generator: Structure {
 
     Rigidbody2D rigid;
     bool isGeneratingPower;
 
     public override void Start () {
     base.Start ();
     actions.Add("Supply Power", () => SupplyPower());
         rigid = gameObject.GetComponent<Rigidbody2D>();
         rigid.angularVelocity = startSpeed;
     }
 }
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
1
Best Answer

Answer by Brocccoli · Mar 28, 2016 at 07:16 PM

I had this same problem when developing the current game I'm working on. I found that a Component based approach to design works incredibly well in Unity, as GameObjects are already made up of a set of components and your scripts should follow this pattern as well.

A component based approach pretty much says that every object you have is the sum of its component parts. Splitting up functionality into different components makes your game incredibly modular and extendible.

So in your case, instead of having this multi-tiered inheritance you could have a component based design with some inheritance mixed in where appropriate.

Following this idea you could have the following.

 public class GamePiece : MonoBehaviour {
   public Dictionary<string, Action> actions;
   public virtual void Start () {
     actions = new Dictionary<string, Action>();
     actions.Add("Destroy", () => Destroy(this.gameObject));
   }
 }

Then your StructureComponent class can look like this.

 [RequireComponent(GamePiece)]
 public class StructureComponent : MonoBehavior {
   private GamePiece myGamePiece;

   void Start() {
     myGamePiece = getComponent<GamePiece>();
   }
 }

Then it would make a lot more sense for any new structure to inherit from your StructureComponent class if need be. Also, the "RequireComponent" tag assures you that when you add the StructureComponent script to your game object, it will automatically add your GamePiece script as well.

You shouldn't be adding to your base classes dictionary, that's not good design in general. Instead you can make different components for your GamePieces that all have an action of their own.

 public class GamePiece : MonoBehavior {
 }
 
 public class StructureComponent : MonoBehavior{
 }
 
 public class StructureWithActionComponent : StructureComponent {
   protected virtual void DoAction() { }
 }
 
 public class GeneratorComponent : StructureWithActionComponent{
   protected override void DoAction() { }
 }

Hope this helps some. Although this example includes inheritance, the inheritance is reserved to components of the same general functionality. So more specific instances of a general component is ok for inheritance, as long as its function can be categorized as being the same as it's base components function with some more specific functionality.

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 DonPerry · Mar 28, 2016 at 07:28 PM 1
Share

Thanks man You have my beans.

avatar image Brocccoli DonPerry · Mar 28, 2016 at 08:22 PM 0
Share

No problem, glad I could help.

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

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

Need help to generate a random assortment of 4 buttons - C# 0 Answers

Check if other.object changes his tag (OnTriggerStay2D) 0 Answers

Saving/loading inherited class scripts (C#) 1 Answer

Why can't I access the public variables of the children class of TestItem in this syntax? I only have access to TestItem variables 1 Answer

Getting KeyNotFoundException and I can't understand why 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