- Home /
OnCollisionEnter not working.
var myZone : int;
function FixedUpdate(){
myZone = this.gameObject.transform.parent.gameObject.GetComponent(EnemyHealth).zone;
}
function OnCollisionEnter(other : Collision){
if(other.gameObject.tag == "Player"){
if(other.GetComponent(Player).zone != 227){
if(other.GetComponent(Player).zone == myZone){
GameObject.Find("SplashScreen").SendMessage("GameOver");
}
}
}
if(other.gameObject.tag == "Enemy"){
Debug.Log("ENEMY : FRIENDLY");
enemyScript = other.gameObject.GetComponent(EnemyHealth);
if(enemyScript.zone == myZone){
if(enemyScript.health <= 0){
GameObject.Find("SplashScreen").SendMessage("GameOver2");
}
}
}
}
Hi. Got this script thats attached to an enemies vision box. If the vision box collides an object tagged player or an object tagged enemy with less than or equal to zero health and in the same zone as its meant to send a message to end the game. It works when it collides with the player. But when it collides with an enemy nothing happens. "ENEMY : FRIENDLY" doesnt even appear in the console so I'm guessing it doesnt register.
The enemy object has a box collider and rigidbody with isKinematic set to true. The visionbox has just a box collider attached. Theyre also tagged as enemy in the editor. The player object has a mesh collider with isTrigger set to true and a rigidbody with isKinematic set to false. It sounds like the player oject shouldnt collide but it does anyway.
So how can I get the second part of my script the part starting other.gameObject.tag =="enemy") to work.Thanks Stealth.
I$$anonymous$$HO, ToLower
or ToUpper
in string comparisions makes life easier:
other.gameObject.tag.ToLower() == "enemy"
Add the following very simple debug:
Debug.Log(other.gameObject.tag);
Answer by AlexGan001 · Jul 28, 2015 at 01:11 PM
Make sure that the enemy tag is the same as the one in the script. Capitals DO matter. if the one in the editor is "enemy" and not "Enemy" that is the problem
Answer by NeverHopeless · Jul 28, 2015 at 05:17 PM
Make sure enemy object is set to a layer whose collision is allowed in collision matrix under Physics setting.