Object Inheritance NullReferenceException: Object reference not set to an instance of an object

0
I have a script called GreedySearch where it contains the following function printMove().

 public class GreedySearch : MonoBehaviour {
     public void printMove(){
         Debug.Log ("GS printMove()");
     }
 
 }

Now I have a main controller called EnemyBrain which defines the following like this:

 public class GreedySearch : MonoBehaviour {
     public GreedySearch gSearch;
 
     public virtual void Start () {
         gSearch = GetComponent<GreedySearch> ();
     }

     public void moveNPC() {
         gSearch.printMove();
     }
 }

Now I have a Enemy script that inheritance EnemyBrain, and I’m trying to call gSearch.printMove(), however I’m getting the error

NullReferenceException: Object
reference not set to an instance of an
object

This is my Enemy script

 public class Enemy1 : EnemyBrain {
     public override void Start() {
         base.Start ();
     }
 
     void FixedUpdate() {
         moveNPC();
     }
 }

The error shows that it comes from EnemyBrain at gSearch.printMove();.

My code does a little bit more, but I think this simple example should be able show where my errors are coming from.

Any help is very much appreciated.

Thank you

You are calling same object from itself:

public class GreedySearch : MonoBehaviour {
    public GreedySearch gSearch;

If this is just a typo then two components EnemyBrain and GreedySearch must be on same gameobject since you are trying to get GreedySearch component without declaring parent:

public virtual void Start () {
    gSearch = GetComponent<GreedySearch> ();
}