[Solved] Instantiating multiple types

I have a turret which is capable of instantiating objects which are either a CannonShell or HomingMissile. Both of these derive from a base class called Projectile.

They are derived from the same base class because I would like to use the same turrets to fire either one depending on how it is set up in the Editor.

The problem is, I want to be able to set a HomingMissile’s target when it is instantiated, but I do not want to have to set a target for a CannonShell, since it will have no use for it, and I plan to devise many more weapons without having to make Projectile support dozens of features, only one of which is used in each one, or to have to break the Launcher every time I come up with a new weapon.

I would ideally like to be able to script

if ( /* the instantiated object has a member called "target" */) {
   instantiatedObject.target = myTarget;
}

Okay, after much thought I realised I was going about it all wrong.

I was trying to bludgeon the Launcher script into supporting multiple instantiated types (which I specifically said I didn’t want to do!), instead of offloading all the work on to the instantiated objects.

Instead of trying to make the Launcher feed the HomingMissile a target, the Launcher simply makes the Target field public and the HomingMissile is responsible for getting the target from the publicly available field in Start(). The CannonShell simply doesn’t do that.