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 /
avatar image
1
Question by ShroomWasTaken · Dec 05, 2017 at 10:29 PM · c#architectureoopsoftware

Interface vs Base Class [C# Code Architecture Problem]

I'm trying to create a simple object selection system for my game. I want each object that should be able to be selected in-game to inherit from the basic selection behavior/functionality.

The first thing that comes to mind is a class or a interface.
So my second thought is, I know for a fact that the selectable objects will always have a boolean value which holds the status of the selectable, wether it's selected or not. I also know that it will have two overridable functions: OnSelected and OnDeselected. I know that both those functions will have a basic functionality; the ability to toggle the boolean value on or off.

Basically, all selectable objects in my game would always have this functionality in them:

 class/interface Selectable {
     bool selected;
     
     OnSelected()
     {
         selected = true;
     ]
 
     OnDeselected()
     {
         selected = false;
     }
 }




With that in mind I decided a class would be best; it can be inherited and can have base functionality, so that I don't have to rewrite the basic functionality for every single selectable object in the game, because that would be a pain in the a**, especially if I would like to add more functionality to the selectables.

After a while of coding I come to the realization that I will need most, but not all of the selectable objects to also be MonoBehaviors. Only one problem, a class can only inherit from a single class in C#, unlike C++ it doesn't support multiple-inheritance. Since not all selectables need to have MonoBehavior functionality I also can't just inherit that in the Selectable class either.

So now I'm thinking of using a interface instead, however I quickly rememeber that interface can only define functionality, not implement it. Eg I could define a function called OnSelected, but I would be unable to implement it in the interface so that all the inherited objects will be guaranteed to have at least that functionality implemented. This means that I would have to rewrite that functionality (which I know that all of the selectable objects will have) for every single selectable in the game.

And now I'm here, wondering if there's any other good way of solving this problem without having to rewrite a bunch of functionality that I know should always be there.



Sorry for the massive amounts of text, didn't even realize I wrote this much... lol

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
2
Best Answer

Answer by Hellium · Dec 05, 2017 at 10:43 PM

Unity is a component oriented game engine, and you should make the most of this. You should not worry about having multiple components on a same GameObject.

This is why I suggest you having an independant component with events so that your other components can react when your object gets selected / deselected.

 using UnityEngine;
 using UnityEngine.Events;
 
 public class Selectable : MonoBehaviour
 {
     [SerializeField]
     private UnityEvent onSelected;
     
     [SerializeField]
     private UnityEvent onDeselected;
     
     public bool Selected
     {
         get ; private set ;
     }
      
     public virtual void OnSelected()
     {
         Selected = true;
         if( onSelected != null )
             onSelected.Invoke();
     }
  
     public virtual void OnDeselected()
     {
         Selected = false;
         if( onDeselected != null )
             onDeselected.Invoke();
     }
 }


Comment
Add comment · Show 3 · 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 ShroomWasTaken · Dec 05, 2017 at 11:41 PM 0
Share

Oh of course, I always seem to fail at this component thing. Ins$$anonymous$$d of using UnityEvents, do you think using Send$$anonymous$$essage would work since the functions will always be on the same GameObject just in a different component? Can't test at the moment because I'm getting a weird stackoverflow error when trying to use Send$$anonymous$$essage and I feel like a UnityEvent isn't needed in my case. :P

Edit: Stack overflow was caused by using the same function name in Send$$anonymous$$essage like the function Send$$anonymous$$essage was called from. Everything seems to be working. Thanks for the help :)

avatar image Hellium ShroomWasTaken · Dec 06, 2017 at 08:14 AM 1
Share

I advise against using Send$$anonymous$$essage. While this is very convenient in your case, refactoring and debugging it is a big pain, because it relies on the function name (string). Because of this, you don't know who called the function using your IDE (With the "Find references" feature), and rena$$anonymous$$g the function name will require to change the string inside the Send$$anonymous$$essage function. But it's up to you ! ;)

Ins$$anonymous$$d, while this is less convenient, I would use the events or a new Interface so that the Selectable component will call GetComponent<ISelectionTarget>().OnSelected() for instance.

avatar image ShroomWasTaken Hellium · Dec 06, 2017 at 08:45 AM 1
Share

Oh I see. Ended up using a GetComponents version of the interface solution since I want to be able to call several ISelectionTargets on a single GameObject:

         ISelectionTarget[] targets = GetComponents<ISelectionTarget>();
         foreach (ISelectionTarget target in targets)
         {
             target.OnSelected();
         }

Again, thank you for the 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

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

An OS design issue: File types associated with their appropriate programs 1 Answer

Is there an elegant way to having single-player class methods within a multiplayer class? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Is it ok to use nested classes, to cache and access components? 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