SendMessageUpwards not working, I dont know why...
I have been following a game tutorial, I have a more PHP with a little C# background and the tutorial uses JS, so to try an learn a bit I have been following the tutorial, and then converting to C#.
I have had a few successes in this conversion, but the problem I am having is calling a script after a raycast hit.
The raycast is working, the debug log script shows that the FPS is within the stated distance, but when I call
hit.collider.SendMessageUpwards ("ApplyDamage", theDamage, SendMessageOptions.DontRequireReceiver);
The method inside an external script 'enemyLogic' is not being called. The script is attached to the 'enemy Object' and in game mode, I can see the health value, I am really stuck here as I have tried as much as I can.
I am still learning Unity, so please be gentle is it is an obvious mistake!
Here are the simple scripts I have so far, meleeAction is attached to the FPS player object, and enemyLogic is a component of 'the enemy' currently a simple cube. meleeScript.cs :
public class meleeScript : MonoBehaviour { // Declare attributes
public int theDamage = 10;
public float maxDistance = 5;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, maxDistance)) {
//if (the raycast distance & position) , ofthedirectionofthenearByObject (is in front of them), (return data to the object hit), and it is less than n 'units' away))
if (Input.GetButtonDown ("Fire1")) {
Debug.Log ("within distance");
hit.collider.SendMessageUpwards ("ApplyDamage", theDamage, SendMessageOptions.DontRequireReceiver);
}
}
}
}
enemyLogic.cs:
public class enemyLogic : MonoBehaviour {
public int health = 100;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
//damage application function (does not seem to call)
void ApplyDamage(int theDamage) {
Debug.Log ("current Health = " + health);
health -= theDamage;
Debug.Log ("New Health = " + health);
}
}
All help and pointers are greatly appreciated