Mario style jump on enemy

I’m making what is at this point a 2D Mario clone and want to send a message initiating a method on an enemy script. The most efficient way I can think to do this would be to do as so.

I have a child object with a box collider and script for ground checking with this C# code.

using UnityEngine;
using System.Collections;

public class MarioFeet : MonoBehaviour {
	
	
	void OnControllerColliderHit(ControllerColliderHit hit) {
		var hitVec = hit.point - transform.position;
		
		if (hitVec.y < 0) {
			Debug.Log("Boop");
			hit.gameObject.SendMessage("MarioStomp");
		}
	}
}

Then on my npc object a sprite renderer, box collider and behavior script:

using UnityEngine;
using System.Collections;

public class npcBehavior : MonoBehaviour {

	void MarioStomp() {
		Debug.Log("Boop-Bop");
	}	
}

I’m not logging what I want but I’m getting a warning, “The referenced script on this Behaviour is missing!”

Do I need to find the game object in my script and get component? I’m not sure exactly how the hit or send message works as I’m new to unity would like to at least know where to look as I’ve found myself alone and down an alley I don’t fully understand.

P.S. this is my first unity question so if there is anything I can do to make my question better I’d love to know.

Try making MarioStomp public

public void MarioStomp()

EDIT:

OK I was on my phone with intermittent connection when I posted that so I went for the short answer in the hope that would fix it so now I have a good connection…

In MarioFeet instead of

hit.gameObject.SendMessage("MarioStomp");

do this, you’ll need to set a script reference at the top of the file first…

private npcBehaviour npcBehaviourScript;

The where the send message is do this (instead)…

npcBehaviourScript = hit.GetComponent<npcBehaviour>();
npcBehaviourScript.MarioStomp();

So it picks up the script attached to the hit enemy and calls the MarioStomp() function.

Really that was shorter than I thought it would be, should have posted it first time.

EDIT:

In fact, thinking about it, you could do it all on one line because you only reference that script once so no point holding onto it. So instead of the SendMessage line…

hit.GetComponent<npcBehaviour>().MarioStomp();

Should work as well.

The warning seems about the script itself, not the method you are trying to call. Sometimes even removing the script from the object and re-adding it does the trick.