- Home /
Other
Collision Detection Issue with SetActive(false) not working
When I throw something at an enemy, it doesn't detect collision. I have a Rigidbody and Capsule collider on the player (Correctly placed on the player). For Enemy I have the same, a Rigidbody and Capsule Collider on the Enemy (Correctly placed on the enemy). I have a Enemy Script on the enemy, but it seems like the collision hits the enemy, but the enemy is never destroyed. I need it to disappear after it is destroyed, but it isn't working right:
public class Enemy : MonoBehaviour { public int health = 1;
public virtual void Hit()
{
health--;
if (health <= 0)
{
Destroy(gameObject);
}
}
Answer by mimmopokemon · Jul 02, 2019 at 06:27 AM
You can use OnCollisionEnter and you can change the tag of the enemy and write in the script: Public void OnCollisionEnter(Collision coll){ If(coll.gameObject.tag == "tagoftheenemy"){ Health -= 1; if(Health <= 0){ Destroy(theplayer) } } }
Sorry for indentation but I'm on the smartphone now. You give the tag to the enemy and change with "tagoftheenemy" and change the player with the gameObject player
I hope you can do this.
Answer by tamarar357_unity · Nov 26, 2019 at 04:51 PM
Thanks for your help! I will try this out too, but I did get it working finally using another method, thanks!
Follow this Question
Related Questions
Player not getting destroyed when touched by enemies 4 Answers
collision wont work 1 Answer
Create hinge joint 2D by collision 2 Answers
Player attacks once but the enemy takes double damages! 1 Answer