This question was
closed Sep 04, 2020 at 03:36 PM by
unity_N4QYDceK2IaDcg for the following reason:
Other
Question by
unity_N4QYDceK2IaDcg · Sep 04, 2020 at 02:57 PM ·
c#
Player shouldn't be destroyed by weapon, but it happens
Hello, i created a player weapon script that should destroy the enemies, sadly before that happens the player gets destroyed immediatly when i start the game.
private void OnTriggerEnter(Collider other)
{
Debug.Log("Name: " + other.transform.name);
if (other.transform.CompareTag("Wall") || other.transform.CompareTag("Monster"))
{
Debug.Log("Name: " + other.transform.name);
GetComponent<Rigidbody>().velocity = Vector3.zero;
Destroy(gameObject, 0.2f);
}
}
private void OnCollisionEnter(Collision collision)
{
Debug.Log(" Name: " + collision.transform.name);
if (collision.transform.CompareTag("Wall") || collision.transform.CompareTag("Monster"))
{
Debug.Log("Name: " + collision.transform.name);
GetComponent<Rigidbody>().velocity = Vector3.zero;
Destroy(gameObject, 0.2f);
}
}
Player is not taged as a Wall or Monster.
Any ideas how to get around this? Thank You.
Comment
Answer by BenWiller1989 · Sep 04, 2020 at 03:20 PM
In both cases you destroy the script holder. If you want to destroy the colliding object, write Destroy(collision.transform.gameObject);