So I have this script, I have a box collider on it, and a box collider on my enemy, but it doesn’t want to work, is there something wrong with my script? I even tried putting a rigidbody on my enemy but to no avail. Not even my first Debug.Log is being called.
var damage : float = 5;
function OnCollisionEnter(hit : Collision){
Debug.Log("Collided");
if(hit.collider.tag == "enemy"){
Debug.Log("Hit Enemy");
AI = hit.collider.GetComponent(NinjaAI);
AI.LoseHealth(damage);
}
}
EDIT: Wow I feel stupid, nevermind I got the first Debug.Log working, so now it collides it just doesn’t recognize the tag for some reason. and yes my enemy is tagged “enemy”.
It’s not tagged “Enemy”, is it?
Also, the collider component of a collision object is different than the actual Collider component, I think, and I’m not sure you can access the usual inherited members, like “tag” from it. I might be wrong though.
Try:
if(hit.gameObject.tag == "enemy"){
Yeah it was tagged “Enemy” but I just changed it to “enemy” with no change. hit.tag returns the error “tag is not a member of UnityEngine.Collision”. And I know hit.collider.tag works because I use it on my bullet script. But the difference is that my bullet has a rigidboy whereas my melee weapons do not because then they either float away(no gravity) or the drop through the ground (gravity).
Try this:
if (hit.collider.gameObject.tag == "enemy")
Or as an alternative you can use CompareTag
Sorry, I posted prematurely, I had edited my post to say hit.gameObject.tag, but you had already read it. You could try giving your weapons rigidbodies and try freezing rotation and/or is kinematic, they shouldn’t float away then.
Does your enemy have a rigidbody? OnCollisionEnter only registers collisions if one or both of the colliders has a rigidbody attached.
Thanks a lot for the tip on the rigidbodys, that had stumped me for a while. I tried CompareTag but that still didn’t work, I really have no idea what’s wrong with it. Thanks for trying to help.
Ok I got it working, I used OnTriggerEnter instead and for some reason the tag (even though I’m sure I did it lowercase) only responds when I match it against “Enemy”. But whatever. Anyway, how I got it to work was I made a debug.log that when collided with something it said "Collided with " + hit.collider.tag, and I found out it was hitting the enemy, but the tag was returning as Enemy. It’s all working now, thanks for the help! 
Here’s the working script in case anyone is interested
var damage : float = 5;
var player : Transform;
function Update(){
Physics.IgnoreCollision(this.collider, player.collider);
}
function OnTriggerEnter(hit : Collider){
//Debug.Log("Collided With " + hit.collider.tag);
if(hit.gameObject.CompareTag("Enemy")){
var AI = hit.collider.GetComponent(NinjaAI);
AI.LoseHealth(damage);
}
}