Question by
TylerStudios · Apr 04, 2016 at 07:30 AM ·
c#collidercollider2dtriggers
(C#) Bullet Damage , OnCollisionTrigger2D not working,(C#) Bullet Damage, Trigger not working.
I am currently developing a game where you have to shoot a enemy to kill it (like most games) and my code isn't working. using UnityEngine; using System.Collections;
public class EnemyHealth : MonoBehaviour
{
public int maxHealth;
public int curHealth;
void Start()
{
curHealth = maxHealth;
}
void Update()
{
if (curHealth < 1)
{
Destroy(gameObject);
}
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Bullet")
{
curHealth -= 1;
Destroy(col.other);
}
}
}
I have read other forum posts about it and nothing has helped.
Before you comment Read this:
Yes I have tried OnTriggerCollison2D but it did not work (I did have trigger on but the bullets passed right thru the enemy and barriers, I did have Kinematic off),
Yes I have all the right tags
Thx
EDIT: Accidentally put OnTriggerCollision, meant OnTriggerEnter ^^
Comment
Best Answer
Answer by Kurtisi · Apr 04, 2016 at 07:41 AM
Have you tried OnTriggerEnter2D instead of OnTriggerCollision2D? Enter part is important.
void OnTriggerEnter2D(Collider2D col)
{
if (col.tag == "Bullet")
{
curHealth -= 1;
Destroy(col.gameObject);
}
}
I've did Destroy(col.gameObject); cause you probably want to destroy the bullet after colliding with it. Hope this helps