Change what script a var holds?

I’m trying to use a single variable to refer to different scripts, depending on what object is using it.

The basic set-up is: The turret, holds a weapon, which holds an ammo type.

The turret and weapon scripts need to reference the ammo script held by the weapon, and a weapon variable will define what ammo type is used (such as bullet or missile). These type differ radically and really can’t be efficiently combined.

However, the weapon scrips share much functionality, and really can’t be split into separate scripts for each type - just for the sake of using a different variable to reference the ammo.

Now despite the vast differences between ammo behavior, they DO share a number of common variables that need to be referenced often, hence the problem.

(The referencing scripts used to be separate, but the code was becoming unmanageable and at this point in development they need to be cleaned up.)

var _script : System.Object;

// typically in Start()
if (condition) {
    _script = gameObject.GetComponent(Bullet);
} else {
    _script = gameObject.GetComponent(Missile);
}

// so I can easily reference the cached component 
_damage = _script.damage;
_range = _script.range;
// etc.

So how do I go about this?

look up inheritance. i don’t know the unityscript syntax.

create a base class for the weapons containing the common variables and also store the weapon type then when you check which component to get, do so by referencing the type.

in your posted code where you check condition, check the type before getting the relevant component - which is only needed for things not in the base class.