MonoBehaviour inheritance problem (168140)

I came across a dilema that I judge to be very simple, but I can’t figure it out, so let’s say we have this:

//Gun.cs
using UnityEngine;
class Gun : MonoBehaviour
{
    void Start()
    {
        print("Print Gun");
    }
}


//Rifle.cs
using UnityEngine;
class Rifle : Gun
{
    void Start()
    {
        print("Print Rifle");
    }
}

I would like to drag the Rifle.cs script into a game object, press run, and have both prints appear on my console.

Is there any way to achieve that? without having to write some code in all my classes that derive from Gun?

To modify and still have access to inherited methods, you need to use “virtual” and “override” in your methods.

 //Gun.cs
 using UnityEngine;
 class Gun : MonoBehaviour
 {
     protected virtual void Start()
     {
         print("Print Gun");
     }
 }
 
 
 //Rifle.cs
 using UnityEngine;
 class Rifle : Gun
 {
     protected override void Start()
     {
         base.Start();

         print("Print Rifle");
     }
 }

The virtual keyword tells “that method can be changed”, the override keyword tells “I am going to change that one”, then the base gave you access to your parents unchanged methods.