Home > Enterprise >  OnCollision Destroy objects
OnCollision Destroy objects

Time:12-05

Hello I know that I have the code right. I want to destroy the material when my player goes on them. I don't know why I can't destroy them. I have put only to my materials box collider with X= 1 Y=1 Z=1.I don't understand why I can't destroy them. The material I gave it also as a tag. Instead of my player destroy those material he pass through them..I have a RigidBody on the player.

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

CodePudding user response:

You need to debug collisions try this function and screenshot both results and material gameobject :

void OnCollisionEnter(Collision collision)
        {
            Debug.Log("all collisions :"   collision.gameObject.name);
            
            if (collision.gameObject.CompareTag("material"))
            {
                Debug.Log("Material collision :"   collision.gameObject.name);
                Destroy(collision.gameObject);
            }
        }
    }

CodePudding user response:

Did you check that the Collider component is contained in the object? And did you make sure that the IsTrigger is not checked on the Collider? If that doesn't work, check if it's running properly with Debug.Log. I'm not sure if Debug is taken properly. Maybe not, but check the basics first.

  • Related