- Home /
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 :) Any help will be welcome.
Answer by whydoidoit · Jul 01, 2012 at 11:19 AM
Try
hit.collider.gameObject.SendMessage("ApplyDamage", 5);
You are sending the message to the calling object at the moment.
i get this error:
Assets/Scripts/Gameplay/mousefire.js(16,37): BCE0019: 'gameObject' is not a member of 'UnityEngine.RaycastHit'.
Perfect, thanks for the speedy reply. You must just be in it for the hugs from random people. :)
Your answer
Follow this Question
Related Questions
casting a raycast from the raycast hit.point 1 Answer
find tag from raycast 1 Answer
Make Raycast ignore anything that "Isn't" my player(Solved) 1 Answer
Raycast Based on GameObject Tag 0 Answers