Unity Destroy(gameObject) only works sometimes
I'm making a platformer game and my player prefab has a rigidbody, a box collider for the top half, and then a circle collider for the bottom half. I'm using a tileset, so I'm using a tileset collider and I have two tilemaps one for the ground, and one for the water (which kills you). The mechanic for water killing works most of the time, but sometimes my player prefab duplicates rather than being destroyed. (Despite Destroy(gameObject) being before calling the respawn method.) The weirdest part is that it happens sometimes, but not all the time. I've tried laying down all of my water again, in hopes I put something down wrong, but it changed nothing. (I know that the water has the correct tag because it instantiates, it just doesn't always destroy) Here's my code.
Death.cs :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class death : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D collider){
if(collider.gameObject.tag == "water"){
Destroy(gameObject);
LevelManager.instance.Respawn();
}
}
}
LevelManager.cs :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LevelManager : MonoBehaviour
{
public static LevelManager instance;
public Transform respawnPoint;
public GameObject playerPrefab;
public GameObject cameraPrefab;
void Awake(){
instance = this;
}
public void Respawn(){
Instantiate(playerPrefab, respawnPoint.position, Quaternion.identity);
}
}
Pictures of the problem, and the prefab's inspector for the water and player:
Player:
Thank you so much!
Your answer
Follow this Question
Related Questions
How can I destroy a Gameobject from a list at a specific index 1 Answer
Does the Destroy function make the game object not exist? 1 Answer
destroying instantiated groups of objects 1 Answer
Mesh Selection in Dropdown?? 2 Answers
Unity2D: How to destroy spawned object once it exit out of camera's view? 1 Answer