I’ve been through the script reference for Collision and some other questions on unityAnswers, and they all lead me to believe that my code should work, but evidently I’ve done something wrong. Take a look.
void OnCollisionEnter(Collision collide)
{
if(collide.gameObject.name == "Hitbox")
{
Destroy(gameObject);
}
}
I know that the collision is being recognized, because if I remove the if statement, the object is destroyed just fine, and if I leave it in, the two rigidbodies still interact physically. They’re both RigidBody/Colliders, so that shouldn’t be a problem anyway. Additionally, I’ve tried a few permutations of “collide.collider.(etc)” to no avail. Hitbox is a prefab I’ve constructed.
I’m kind of embarrassed that I’m having so much trouble with this, but clearly I’m missing something here. Thanks a lot for any help you can provide.
Edit:
I was able to create a workaround by assigning a tag to the Hitbox prefab and checking for that instead like so:
if(collide.gameObject.tag == "Hitbox")
I’m not sure if this will cause problems for me later, but it seems to work for now.
Edit 2:
It turns out that object’s name was being processed as “Hitbox(Clone)”. I followed the advice of an answer here and had the name written to the debug log.