- Home /
c# question, Hiding an inherited class reference
So basically i have a class (Pickable : Monobehavior) which has a reference to a (Targeter targeter) I have another class (Teleporter : Pickable) which inherits from the Pickable class, but i want to replace the (targeter) references to be from another class (ThrowTargeter : Targeter) which inherits from the targeter but has additional functions and parameters which im using for the Teleporter that i dont use for other Pickable objects that use the regular targeter.
I access these parameters from the ThrowTargeter class in my Teleporter by casting the targeter as ThrowTargeter, this is not problematic to me since I'm manually assigning a ThrowTargeter object to the targeter reference so the cast never fails and the script is working fine.
of course when i try to access the ThrowTargeter exclusive parameters from my targeter reference without casting it gives an error, but i realized i can skip the casting process by making a new reference to the targeter as a ThrowTargeter which hides the inherited targeter reference. this removes the error that i would usually get from visual studio if i try to access the parameter that is exclusive to (ThrowTargeter) from my targeter reference without casting it as (ThrowTargeter) first, as a matter of fact it shows me that a cast is redundant when i do so.
this seemed like a cleaner way to do it, as i wouldn't have to cast it everytime i want to access it. The problem is that when i go to unity, it doesn't actually work, the reference from the inspector still shows that the (targeter) is a (Targeter), and when i assign a ThrowTargeter in its place, it displays errors when the code gets to the part where i use functions from the (ThrowTargeter) (as i mentioned before it worked fine when i accessed those functions by casting the targeter as ThrowTargeter, but when i hide the inherited Targeter with a ThrowTargeter the functions don't work despite visual studio being okay with it and even showing me that a cast is not necessary and is redundant)
Is this something that has to do with Unity or am I not understanding how hiding inherited members work in c#? Also whats the right approach to do this? as i said the code is perfectly fine while using casting, I'm not a professional developer but my gut feeling tells me thats now how i should use casting (right now im only accessing the targeter twice in the Teleporter so its not a big deal, but i imagine in a larger script where that reference is accessed more frequently, it would probably be annoying).
I hope my description was explanatory enough :)
Answer by DerpWyvern · Apr 30 at 02:05 AM
Update : I replaced the new ThrowTargeter targeter reference that was supposed to hide the inherited Targeter targeter with a custom getter that hides the inherited targeter and returns it already casted as ThrowTargeter like this
ThrowTargeter targeter => base.targeter as ThrowTargeter
this seems to work fine as i don't have to cast the targeter everytime i need it and it works fine in unity. I'd still love to hear your opinions on the issue and I'd love to hear an explanation of why visual studio had no problem with me hiding the inherited member and treating as a different class but Unity was not.