I’m trying to use SendMessage in conjunction with OnCollisionEnter to detect enemies being hit, and applying damage to them. Here is my code.
- Attached to player’s weapon
using UnityEngine;
using System.Collections;
public class enemyKill : MonoBehaviour {
void Start () {
}
void Update () {
}
void OnCollisionEnter(Collision hit)
{
if(hit.gameObject.tag == "enemy"){
//detects if object hit was an enemy
SendMessage("applyDMG");
//sends messaage
print("you hit him");
//bugtesting that collision was detected
}
}
- Attached to enemy
using UnityEngine;
using System.Collections;
public class receiveDamage : MonoBehaviour {
void Start () {
}
void applyDMG(){
//receives message? not 100% how this works pls help
print("DMG message received");
//bugtesting message received
}
}
When the player’s sword collides with an enemy, the message is not received, but “you hit him” is printed in the console, sop the collision is working.
Please help, thanks.