Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 NosTurtle · Jul 01, 2017 at 07:06 AM · inheritanceconversionclass instance

How would I handle having one instance of 2 possible class, one derived from the other in a Monobehavior?

I know that may sound odd, so let me elaborate.

I am creating a custom character controller, but with 2 separate versions, a basic type & an advanced type, advanced inheriting from the basic. They both have a classes inside them, AirClass & AirClassExt respectively: AirClass has the basics of handling aerial properties while AirClassExt gives more options, as is the point of having these two versions of the controller. For the sake of not having multiple instances of these air properties in the inspector & the code, I would like to do as follows, with just one instance of either class, depending on which monobehavior is attached:

 using UnityEngine;
 using System.Collections;
 
 public class ControllerBasic : MonoBehaviour
 {
     public class AirClass
     {
 
     }
     protected AirClass _air = new AirClass ();
 }
 
 public class ControllerAdvanced : ControllerBasic
 {
     public class AirClassExt : AirClass
     {
 
     }
     //Convert (cast) the original class (_air) to the extended class
 }



Maybe this isn't the best way to do it but this is what I want to achieve. Any help would be appreciated.

[EDIT] This picture shows why I want to do it this way, as having several instances of similar classes will show doubles in the inspector & make for confusing code: alt text

-Thank you.

air.png (26.5 kB)
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 andrew-lukasik · Jul 01, 2017 at 08:24 AM 0
Share

One way to do it would be having 2 fields of different type but pointing to the same instance:

 public class ControllerBasic : $$anonymous$$onoBehaviour
 {
     protected AirClass _airBasic = new AirClass();
 
 
     protected virtual void Awake ()
     {
         
     }
 
     public class AirClass
     {
 
     }
 }
 
 public class ControllerAdvanced : ControllerBasic
 {
     protected AirClassExt _airExtended;
 
     protected override void Awake ()
     {
         base.Awake();
 
         _airExtended = _airBasic as AirClassExt;
     }
 
     public  class AirClassExt : AirClass
     {
 
     }
 }
avatar image NosTurtle andrew-lukasik · Jul 01, 2017 at 06:26 PM 0
Share

Well, this helps with the code aspect of it, having one property for either class, but I am still having my expected problem in the inspector doing it this way, now showing 2 different versions of mostly the same class, as pictured: alt text

air.png (26.5 kB)

1 Reply

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

Answer by andrew-lukasik · Jul 01, 2017 at 07:22 PM

Maybe this way then:

 /// <summary>
 /// Implements common functionalities
 /// </summary>
 public abstract class Controller : MonoBehaviour
 {
     #region protected_methods
 
     /// <summary>
     /// Does something basic
     /// </summary>
     protected void DoSomething ( AirClass air )
     {
         
     }
 
     /// <summary>
     /// Does something more than basic
     /// </summary>
     protected void DoSomething ( AirClassExt air )
     {
         //do basic stuff:;
         DoSomething( air as AirClass );
 
         //
         // code doing more than basic goes here
         //
     }
 
     #endregion
     #region nested_data_types
 
     /// <summary>
     /// Basic air data
     /// </summary>
     [System.Serializable]
     public class AirClass
     {
 
     }
 
     /// <summary>
     /// Extended air data
     /// </summary>
     [System.Serializable]
     public  class AirClassExt : AirClass
     {
 
     }
 
     #endregion
 }


 public class ControllerBasic : Controller
 {
     #region fields
 
     [SerializeField] protected AirClass _air = new AirClass();
 
     #endregion
     #region monobehaviour_methods
 
     void Start ()
     {
         DoSomething( _air );
     }
 
     #endregion
 }
 

 public class ControllerAdvanced : Controller
 {
     #region fields
 
     [SerializeField] protected AirClassExt _air = new AirClassExt();
 
     #endregion
     #region monobehaviour_methods
 
     void Start ()
     {
         DoSomething( _air );
     }
 
     #endregion
 }


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 NosTurtle · Jul 01, 2017 at 08:16 PM 0
Share

Ah, nice. I was thinking too linearly, with one inheriting from the other only, I didn't think about both inheriting from a all together different base class. This also expands my knowledge of what the abstract modifier can really do. I really only used it as accessible containers for useful, widely used methods...

I do have to head to work soon, but I will give this a try as soon as I can.

avatar image NosTurtle · Jul 06, 2017 at 07:25 AM 1
Share

Hey again. I do apologize for the late response. I had quite a busy weekend but am making good progress. What you have suggested here seems to work out nicely in my situation. While I have yet to complete what I am working on, it has proved to be a good solution. Thank you for taking the time to help me.

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

66 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

Related Questions

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

Saving class data without having lots of references? 1 Answer

Does attaching a derived class script to a gameobject create a new instance of its parent class specific to that gameobject? 0 Answers

How to choose base class to make Inherit C# 2 Answers

C# Inheritance and passing Components 0 Answers


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