Calling parent script method from child object script

Hi all

I have a parent object (Parent_Obj) which has a script which moves it about the place.

I have a child object (Child_object) attached to it which is empty and has a 2D collider attached and a script. From that script I want to call a method in the script on the parent object.

The parent object script has this in it;

public void StopNPC()
{
print("TEST");
}

The child script has this;

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.collider.name == "Player")
        {
            base.StopNPC();
        }
    }

However the base.StopNPC() line isn’t working, The error is

Assets\CheckCollision.cs(26,18): error CS0117: ‘MonoBehaviour’ does not contain a definition for ‘StopNPC’

What’s the correct way of calling a method of a script attached to a parent object?

I’ve also tried;

 private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.collider.name == "Player")
        {
            NPCWander ParentScript = this.transform.parent.GetComponent<NPCWander>;
            ParentScript.StopNPC();


        }
    }

But that didn’t work either.

Almost got it. GetComponent is a function, so it needs parentheses:
NPCWander ParentScript = this.transform.parent.GetComponent(); // empty parentheses at end

The “this” is implied and can be left out. Also if you just want to run the function once, you can call it directly:
transform.parent.GetComponent().StopNPC();

The same can be used to set variables in the other script:
transform.parent.GetComponent().myBool = true;

Sometimes you want to cache a local reference to the script so you can use it multiple times. The way you’ve done it, it’s local only to the OnCollisionEnter2D function. Instead, you can do it outside any function (at the global script level) and get the reference in Start():

NPCWander ParentScript; // this will be accessible in any function

void Start() {
ParentScript = transform.parent.GetComponent();
}

Hope that helps!

1 Like

It’s a natural confusion. There are only so many good words, and we re-use parent/child for two things. If OrcScript inherits from MonsterScript sometimes we call that a parent and child, since the technical terms (superclass and subclass) are longer. That’s what “base.” is for – looking at your superclass (you will probably never need it). It’s a basic thing in C#, or any Object Oriented language.

Meanwhile, having a parent gameObject is a completely different thing, which Unity set-up and works the way it does because Unity said so.

I’m having a similar problem, and used the same solution as in this thread, but I still get the an error.

In my child script I wrote:

transform.parent.GetComponent().printToConsole();

In this case, “MainScript” is the name of the script in the parent object, and “printToConsole()” the name of the method in the script.

I get the following error.
The type or namespace name ‘MainScript’ could not be found (are you missing a using directive or an assembly reference?)