Collision not working

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class destroyif : MonoBehaviour
{
    void Update()
    {
        destroy();
    }

    void destroy()
    {
        void OnCollisionEnter(Collision collision)
        {
            if (collision.gameObject.tag == "Tree")
            {
                Destroy(gameObject);
            }
        }
    }
}

This code is meant to stop trees from spawning on top of each other but its not working. I made use to add colliders and the tags.
Can anyone help.

Several things:

Your destroy() method doesn’t actually do anything. It defines a local method called OnCollisionEnter, but doesn’t actually execute any code. In essence, none of your code actually does anything.

OnCollisionEnter is meant to be a top-level method on your class, like your destroy() and Update() methods. Unity will call the method automatically if a collision is detected

If your trees have colliders and no rigidbodies OnCollisionEnter will not be called. At least one object in the collision needs to have a rigidbody.

Both trees in the collision will receive the OnCollisionEnter callback. Meaning both of your trees will be destroyed if you set this up properly.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class destroyif1 : MonoBehaviour
{

void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == “Tree”)
{
print(“collision”);
Destroy(gameObject);
}
}
}
so like this?
and I added rigidbody to all of the trees


it doesn’t work.
i get this error:

That error is telling you to either use a convex collider or make your Rigidbodies kinematic.

6706429--770440--upload_2021-1-9_15-56-20.png
still doesn’t work

It actually does, I just tested it.

But beware, it lets you spawn ALL the trees, THEN all the collision messages are processed AFTERWARDS, during the physics update portion of the game loop.

Here is some timing diagram help:

https://docs.unity3d.com/Manual/ExecutionOrder.html

ALSO, when you post code, please use code tags: https://discussions.unity.com/t/481379

When you post code properly formatted, statements like this:

can more clearly show you are destroying the object that this script is on, not what it is hitting. Is this what you want? Also, in my test, two objects spawned on top of each other (with Rigidbodies and convex colliders) were BOTH told that the other one spawned on them, so without further checking they would blindly Destroy() each other.

I also did NOT test the case of three objects at once. It might not do what you expect.

If you are trying to explicitly deconflict something, just keep a list of what you have spawned and check for distance and explicitly despawn it. You’re likely to have a lot of problems relying on edge case physics behaviors.

1 Like

For me they just spawn on top of each other i dont know why.

6707305--770581--upload_2021-1-9_21-42-4.png

6707305--770584--upload_2021-1-9_21-42-14.png