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