- Home /
Deal Damage On Collision
I am trying to make the player take damage when touching but i can't. This is my code: public void OnCollisionEnter()
{ if (GameObject.FindWithTag == ("Enemy"))
{ DealDamage(5); } }
You really didn't give us anything else other than that. The problem could be in the collision, the deal damage script, the rigidbody missing, honestly pretty much anything. When asking a question, you should give full details related to your issue.
Answer by Xepherys · Sep 02, 2017 at 03:51 PM
OnCollisionEnter() requires parameters.
Something like
public void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Enemy")
//do stuff
}
As your code is written, you aren't sending params to OnCollisionEnter, so it doesn't have any reference, and you aren't looking at tags for objects in the collision, you're looking for ANY GO that has an Enemy tag.
Answer by spidermancy612 · Sep 02, 2017 at 04:04 PM
FindWithTag is a function that looks through the whole scene to find an object with the specified tag, whereas if you're trying to find if an object is touching another with a specific tag then you'll want to use either CompareTag("myTag") or collider.gameObject.Tag == "myTag".
void OnCollisionStay (Collision col) {
if (col.compareTag("myTag") {
DealDamage(5);
}
}
Alternatively if you want to do it through entering a trigger then you can use:
void onTriggerStay (Collider other) {
if (other.compareTag("myTag") {
DealDamage(5);
}
}
Your answer
Follow this Question
Related Questions
weapon collision with characters 1 Answer
Character controller mess up damage 3 Answers
Damage on collision problems... 0 Answers
Particles not always colliding 1 Answer
Velocity data from OnCollisionEnter is delayed (incorrect) 3 Answers