How do I reference a generic BaseClass using GetComponentInParent<>()

I am creating a tool that will allow the user to make a tile map of hexagonal tiles or square tiles or really any tiles that will tile together mathematically. I am using a base class called TileScript that is generic (I’ll admit my understanding of generics is shaky at best) the script looks like this:

public class TileScript<T> : MonoBehaviour where T : MonoBehaviour
{
    public Vector3 gridPosition;

    private void Awake()
    {
        if (Application.isPlaying)
        {
            GetComponent<T>().enabled = false;
        }
        else
        {
            GetComponent<T>().enabled = true;
        }
    }
}

The Awake function disables the class in play mode so it doesnt weigh down the running application and this is because the tool is mainly for use in edit mode, it has other functions too but to keep it short I’ll edit my classes to the necessary info. The problem comes when I have a child game object to this game object that needs to get the gridposition. In the derived class I update the grid position:

public class HexTileScript : TileScript<HexTileScript>
{
    protected Vector3 ErrorCheckingPosition;
         
    public override void UpdateGridPosition()
    {
        gridPosition.y = transform.position.z / 1.5f;
        gridPosition.z = transform.position.y / Settings.Instance.zSnap;
        gridPosition.x = Mathf.RoundToInt(transform.position.x / (Mathf.Sqrt(3f) / 2.0f)) / 2.0f;
    }
}

then in a child object, i am trying to reference the grid position:

public class TileMesh : MonoBehaviour
{ 
    private Vector3 oldPosition;

    private void Start()
    {
        DrawTile();
        oldPosition = GetComponentInParent<HexTileScript>().gridPosition; // <--- THIS WORKS
        oldPosition = GetComponentInParent<TileScript>().gridPosition; // <--- I NEED THIS AND IT DOESNT WORK
    }
}

I want to be able to reference this by the base class so this function will work regardless of what type ends up inheriting from it but i cant seem to figure this out.

Visual Studio gives me an error: “Using generic type ‘TileScript’ requires 1 type arguments”

i have tried

oldPosition = GetComponentInParent<TileScript<T>().gridPosition;
oldPosition = GetComponentInParent<TileScript<gettype(transform.parent)>().gridPosition;

I didn’t expect either of those to work but hopefully that shows you how truly lost I am.

If I am going in the totally wrong direction then please let me know!

I am thinking that interfaces may be something I am overlooking for this but I know very little about this and I’m just trying to get it to work.

Thanks!

For this you would want to have a non-generic base class that holds as much data as it can, which the generic class will then inherit from and fill in the rest, i.e.

public class TileScript : MonoBehaviour
{
    public Vector3 gridPosition;
}

public class TileScript<T> : TileScript where T : TileScript
{
    private void Awake()
    {
        if (Application.isPlaying)
        {
            GetComponent<T>().enabled = false;
        }
        else
        {
            GetComponent<T>().enabled = true;
        }
    }
}

From there, you can use get component on the base class and cast down when necessary;

oldPosition = GetComponentInParent<TileScript>().gridPosition;

(GetComponentInParent<TileScript>() as HexTileScript).UpdateGridPosition();

This is a common misconception of generic classes. The type requires an additional type parameter which is bound at runtime. However generic types with different type parameters are completely different types. They have nothing in common in the OOP sense. For example a List<string> and a List<int> are just like a ClassA and a ClassB. The two concrete list types just share the same structure and code basis but are not compatible in any way.

There are ways to get some sort of compatibility of types with generic parameters called covariance and contravariance. However those only apply if the type arguments are strictly used either for input OR for output. The type arguments have to be declared explicitly as “out” or “in” parameters in this case. For more information see co- / contravariance. Though those are very specific and limited cases.

Since you actually want to have a common base class you shouldn’t use a generic class at all. There’s also no need for a generic class as I mentioned in the comment to NameyS’s answer. So you may simply use a base class like this:

public abstract class TileScript : MonoBehaviour
{
    public Vector3 gridPosition;
    public abstract void UpdateGridPosition();
    private void Awake()
    {
        enabled = !Application.isPlaying;
    }
}

public class HexTileScript : TileScript
{
    protected Vector3 ErrorCheckingPosition;
    
    public override void UpdateGridPosition()
    {
        gridPosition.y = transform.position.z / 1.5f;
        gridPosition.z = transform.position.y / Settings.Instance.zSnap;
        gridPosition.x = Mathf.RoundToInt(transform.position.x / (Mathf.Sqrt(3f) / 2.0f)) / 2.0f;
    }
}

ps: I just want to repeat that generic classes are in some way the opposite of inheritance. They allow you to create seperate classes without rewriting or deriving a new class from a base class. A great clue is a class like this:

public class Test<T>
{
    public static Test<T> instance;
}

Static variables only exist once per class. However in this case you can have as many “instance” fields as you like. Just use different type arguments and you get a new, seperate class each time. So Test<bool>.instance is completely different from Test<GameObject>.instance. The different Test classes are all unique classes and not compatible to each other. By deriving a new class from a generic class and passing itself as type argument completely isolates the class. Just imagine the generic angle brackets are not there. So creating a class like this:

public class MyClass : Test<MyClass>
{
}

essentially creates two new destinct classes. First theres MyClass and the base class is “TestMyClass”.