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.
Are you comfortable sharing the project so I can take a look and see why the renderer isn't swapping materials in that block as it should?
– VicarianSure, how should I send it? @Vicarian
– ProjectM23You can upload it to a sharing site like Mega or MediaFire if you don't want to use DropBox, Drive, or OneDrive. Just put a link to it in a comment and I'll have a look.
– VicarianHeres the link: http://www.mediafire.com/file/5rgv8t0092t3k1x/RayCastProject[send].rar
– ProjectM23Couple of weird edge cases, got them handled - my test was done using OnTriggerEnter and it just worked. I mixed up the order of the logic a bit, but got it fixed.
– Vicarian