- Home /
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:
-Thank you.
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
{
}
}
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:
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
}
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.
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
![](https://koobas.hobune.stream/wayback/20220612125253im_/https://answers.unity.com/themes/thub/images/avi.jpg)