Collider deletes ground with/destroy game object

When I add a Destroy(gameObject) and Destroy(other.gameObject) script to my characters it also takes out the ground plane that I added but I don’t have is trigger checked on the ground plane.

Is there a way to exclude the ground plane from getting destroyed?

I think we are going to need more information to help here. Can you please post your code (using the </> button)? It may also help to show a screenshot of the ground object's inspector tab.

Just using a simple on trigger collider that I add to the attackers coming at the player in the game. If I raise the attacker off the ground (not the place I want it to start but just proves to me that it is the problem) when gravity pulls it down to land on the ground plane it deletes the ground plane. public class DetectCollisions : MonoBehaviour { private void OnTriggerEnter(Collider other) { Destroy(gameObject); Destroy(other.gameObject); } }```

I wrote how i have it in the answer to my question below not the actual answer lol

2 Answers

2

Just using a simple on trigger collider that I add to the attackers coming at the player in the game. If I raise the attacker off the ground (not the place I want it to start but just proves to me that it is the problem) when gravity pulls it down to land on the ground plane it deletes the ground plane.

public class DetectCollisions : MonoBehaviour
{
    private void OnTriggerEnter(Collider other)
    {
        Destroy(gameObject);
        Destroy(other.gameObject);
    }
}```

Your code is indiscriminate and will just destroy anything it comes into contact with. Usually there is some check to determine if the object you touched is something that should be destroyed.

One popular option is to use tags:

//This assumes you have created a "Player" tag and assigned it to the player object.
if(other.gameObject.CompareTag("Player"))
{
    Destroy(other.gameObject);
}

You could also use different Layers for the ground and other objects. You can control which layers will interact with other layers in the Project Settings. So you could set the layer interaction matrix such that the enemy layer never interacts with the ground layer.

Or you could check the layer of the other object to see if it is something that should be destroyed:

//Again, this assumes you have created a separate layer for things other than the
//ground that you want to be destroyable by your attackers.
if(other.gameObject.layer == LayerMask.NameToLayer("KillableObjects"))
...

Thank you so much this is super helpful, I'm still new in the journey. Got stuck and now I have something new to research! Thanks again!