Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
0
Question by CameronMorrow · Oct 05, 2020 at 09:09 PM · getcomponentinheritancegenerics

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!

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 unity_ek98vnTRplGj8Q · Oct 05, 2020 at 09:32 PM 2
Share

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();
avatar image CameronMorrow unity_ek98vnTRplGj8Q · Oct 05, 2020 at 09:41 PM 0
Share

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!

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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();
Comment
Add comment · Show 1 · 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 Bunny83 · Oct 06, 2020 at 01:26 AM 0
Share

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.

avatar image
0

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".

Comment
Add comment · 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

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

146 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

Related Questions

Is it possible to set variables for classes? 2 Answers

C# Generic Type Inheritance 2 Answers

GetComponent returns null when using a generics class for T 1 Answer

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

When or where do I set a prefab script subclass property? 2 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