Accessing inheriting scripts

Situation:
I have two scripts, one named WeaponCore and another names AssaultRifle which inherites from WeaponCore. WeaponCore has a function in it, that is called “GetAmmo”.

Problem: Now I want to scan a transform if there’s a component attached, which inherites from WeaponCore(because maybe the player holds one which has a script named “Pistol” or s. e. attached). For this, I use foreach and GetComponents(WeaponCore)

Question1: So, how can I call functions of a component, of which I don’t now the type/name?

Question2: If a use GetComponent(WeaponCore) on a transform, which has “AssaultRifle” attached, does it return nothing or the AR-Script?

I can C# and Unityscript, so feel free to use the language you want^^

You can use the keyword is to test an object for its type before you attempt to call its functions. It is used like this:

if (GetComponent<WeaponCore>() is Pistol)
{
    // Call Pistol method
}

I assume WeaponCore is a script that inherits from MonoBehavior, right? If that is so, then AssaultRifle or Pistol also inherit from MonoBehavior, since their base class does. Therefore, if you use GetComponent on a GameObject that has AssaultRifle attached, it should return AssaultRifle, because AssaultRifle is of the same type of WeaponCore due to the inheritance. I.e. when Unity does a search for components of type WeaponCore behind the curtains, it’ll test true against a component of type AssaultRifle.

To be honest my preference would be to have just a single Weapon script which has state changes eg

var isPistol : boolean;
var isAssualtRifle : boolean;
var pistolAmmoLimit : int = 30;
var assualtRifleLimit : int = 7;
var pistolAmmo : int = pistolAmmoLimit;
var assualtRifleLimitAmmo : int = assualtRifleLimit;
var pistolObject : GameObject;
var assualtRifleObject : GameObject;



function GetAmmo(){

if (isPistol){
//Do something
}

if (isAssualtRifle{
//Do something
}

function Fire(){

if (isPistol){
    //Do something
    }
    
if (isAssualtRifle{
    //Do something
    }    
}

}

You question is a bit confusing. You want to call a function but you don’t know which name the function has? That doesn’t make much sense to me. The advantage of inheritance is that you implement a function GetAmmo in your base class and make it “virtual”. Now any derived class can override the function. If you access a derived class (eg your AssaultRifle) via base-class-reference (WeaponCore) you can call the function GetAmmo() (declared in WeaponCore) but it actually will invoke the overridden function.

public class WeaponCore : MonoBehaviour
{
    public virtual int GetAmmo()
    {
        return 0;
    }
    public virtual void Fire()
    {
        // The base class can't fire
    }
}

public class AssaultRifle : WeaponCore
{
    private int m_Ammo;
    public override int GetAmmo()
    {
        return m_Ammo;
    }
    public override void Fire()
    {
        // Implement here how the AssaultRifle should fire
    }
}

// Somewhere else
WeaponCore[] weapons = GetComponents<WeaponCore>()
foreach(WeaponCore weapon in weapons)
{
    if (weapon.GetAmmo()>0)
        weapon.Fire();
}