Applying damage to an object

My character needs to interact with items. In this case, he needs to be able to cut down trees. Most of the code for it i have worked out, the bit i am stuck on is how to apply damage to an object when clicked on.

So far i have this:

function Update () {

var hit : RaycastHit;

if (Input.GetButtonDown("Fire1")) {
	if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit)) {
		Debug.Log(hit.collider.tag);
			if(hit.collider.tag == "Tree") {
				Debug.Log ("tree");
				SendMessage ("ApplyDamage", 5.0);
			}
		}
	}
}

with this attached to the tree object

function ApplyDamage (damage : float) {
    print (damage);
    }

However, although it can detect that the item is a tree on a click, it throws up the error:

SendMessage ApplyDamage has no receiver!
UnityEngine.Component:SendMessage(String, Object)
mousefire:Update() (at Assets/Scripts/Gameplay/mousefire.js:16)

I will be missing something here, still finding my way around code :slight_smile: Any help will be welcome.

Try

 hit.collider.gameObject.SendMessage("ApplyDamage", 5);

You are sending the message to the calling object at the moment.