In regards about Object Inheritance and Null error

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

publicclassGreedySearch:MonoBehaviour{
    publicvoid printMove(){
        Debug.Log("GS printMove()");
    }
}

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

publicclassGreedySearch:MonoBehaviour{
    publicGreedySearch gSearch;
        publicvirtualvoidStart(){
            gSearch =GetComponent<GreedySearch>();
        }
    publicvoid 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

publicclassEnemy1:EnemyBrain{
    publicoverridevoidStart(){
        base.Start();
    }
    voidFixedUpdate(){
        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

Your spacing got lost I think.

Your first two scripts are both named “GreedySearch”, or you haven’t shown the class “EnemyBrain”. Beyond that, it is really hard to tell what is going on, you will need to provide more information/scripts.

Hello zombiegorilla, to make the long story short, I wanted to call moveNPC from Enemy1, which inherits EnemyBrain. However,when I called the function moveNPC from Enemy1, it shows that there’s a Null reference at EnemyBrain Line7 (which is gSearch.printMove();).

Is it possible to call moveNPC without redefining gSearch = GetComponent() within the script Enemy1? I don’t want to have 4 Enemies and each of them having an individual EnemyBrain script within them…

This is a uni assignment, and the reason I didn’t post all the code up here is because 1) my code looks really ugly at the moment. 2) I don’t want my professor, or maybe the forum community, to think that I’m actually asking someone else to “do” my work.

If this explanation that I’ve provided still isn’t clear enough, then I guess I will upload some more code.

By the way, this is really my first go with Unity3D due to school requirements which is why my coding logic/design might be weird.

Thanks again for your help.

Have you verified that EnemyBrain.Start is called upon initialization and that GetComponent() doesn’t return null?

Hello KelsoMRK, I have confirm that the base start is being called upon initialization, and GetComponent does get call in EnemyBrain, however, it doesn’t seem to be recognized if I use a function within Enemy1, that inherit EnemyBrain. If anyone is willing to have a deeper look at my code, I’m more than happy to share it via a link. Like mentioned before, it’s kinda ugly at this stage as alot of trial and errors is going on.

Thanks for reply

What you are trying to do is certainly possible. Without seeing the actual code being used, it’s difficult to tell where the problem lies. More than likely the problem is just something simple.

@zombiegorilla I have sent you a msg in your inbox. please have a look when you are free. thanks