- Home /
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!
I'm not sure you need to use generics here, good old inheritance and polymorphism should do the trick (if it doesn't let me know where I'm mistaken). This is how I would do it though...
//No generics here, just a base class with an abstract function
public class TileScript : $$anonymous$$onobehavior {
public abstract void UpdateGridPosition();
}
public class HexTileScript : TileScript {
public override void UpdateGridPosition(){
//blah blah blah
}
}
//Then in a child script you can just call
GetComponentInParent<TileScript>().UpdateGridPosition();
I'm going to try this, I honestly can't remember why I didn't do this to begin with but if it works I'll let you know!
So, yes I didnt need to use generics at all! hopefully I don't come across a reason to convert it back. You are awesome thankyou so much!
Answer by Namey5 · Oct 05, 2020 at 11:01 PM
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();
What's even the point of the generic parameter in this case? The GetComponent call is complete nonsense since it would just return the same reference as "this". So the non generic base class can simply do
public class TileScript : $$anonymous$$onoBehaviour
{
public Vector3 gridPosition;
private void Awake()
{
enabled = !Application.isPlaying;
}
}
ps: Be aware that GetComponentInParent does first search the own gameobject for this component and if not found it will crawl up the transform hierarchy. This works similar to GetComponentInChildren which will also find components on the source gameobject.
Answer by Bunny83 · Oct 06, 2020 at 02:49 AM
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".
Your answer
Follow this Question
Related Questions
Getting variables by their names? 3 Answers
NullReferenceException. GetComponent dose not work properly 2 Answers
How do I access a script component added to a gameobject from an assembly at run-time 0 Answers
Why use generics GetComponent() and what are generics 1 Answer
Why Doesn't Getcomponent add script to game objects inspector automaticly? 1 Answer