Unity inheritance, is this possible?

I am trying to create a class which is weapon , which makes lots of weapons.

I want to make it so just a weapon is attached to the player which can be any weapon

using UnityEngine;
using System.Collections;

// base class for all weapons

public abstract class WeaponTypes : MonoBehaviour{
	    	    	
	// Use this for initialization
	public virtual void SetUp () 
	{
		
	}

the other weapon is this.

using UnityEngine;
using System.Collections;

class MiniGun : WeaponTypes {

// Use this for initialization
public override void SetUp () 
{		
	base.SetUp();

       // I want to call this
	
}

And this is called by this.

WeaponTypes WT = new MiniGun();
WT.SetUp();

Currently after breakpointing the line of code above , WeaponTypes WT is allays null. and the SetUp is never called.

Exactly as @DaveA wrote - don’t use constructor for MonoBehaviour. To achieve what you want, you have to create script for your player, e.g.

using UnityEngine;

public class PlayerScript : MonoBehaviour
{
    public WeaponTypes weapon;

	public void Start()
	{
        weapon.SetUp();
    }
}

and attach this script to your player object. Then you have to attach MiniGun script to some weapon object, and finally you have to select player object and in inspector assign a weapon to it (e.g. by dragging MiniGun object to Weapon field).

Starting game should execute derived and base SetUp methods.

MonoBehaviors like to live on game objects and be initialized by Start or Awake (not ‘new’). So treat it like that and probably call Setup from Start

Yes, it’s totally possible. Just keep in mind that the Unity equivalent to a constructor is the Awake() method, which get’s called from the constructor of a MonoBehaviour internally.

  public abstract class WeaponTypes : MonoBehaviour {
    public virtual void SetUp () {
      print("I'm a weapon"); 
    }

    void Awake() {
      SetUp();
    }
  }

Then this would be your class which inherits from WeaponTypes:

  public class MiniGun : WeaponTypes {

     public override void SetUp () {     
         base.SetUp();
         print("And also I'm a minigun");
     }
  }

Also, you mustn’t call “new MiniGun()”, as it’s not allowed to create MonoBehaviours this way. Instead, instantiate some Prefab which has the MiniGun component:

public MiniGun myPrefab;
...
MiniGun newGun = Instantiate(myPrefab);