Issue with collisions on child layers when using prefabs?

Hi
I have a problem. Might be a bug
I have a gameobject (rock) with a spherecollider. This is in layer “Collectables”.
This gameobject have a empty childobject (RangeCollision) with its own spherecollider. This is in layer “RangeTriggers”.

I have an object which must react to collision with the rock (But not the rangecollider), which is placed in layer “TriggerTargets”.
I have then used the “layer collision matrix” to make sure that “RangeTriggers” does not collide with “TriggerTargets”.

This is the code for the object which must react when the “Rock” collides:

void OnCollisionEnter(Collision collision)
    {
        Collectable newCollectable = (Collectable)collision.collider.gameObject.GetComponent(typeof(Collectable));

        if (newCollectable != null)
        {
            addWeight(collision.collider.rigidbody.mass);
            newCollectable.DestroyMe();
        }
        
    }

When drag my prefab from project ressources the above code works every time.

But if i copy one of the prefabs using ctrl-c/v then it only works approx 50% of the times. The other times the collider in OnCollisionEnter is the spherecollider of the childobject (Which should not collide, as set in the collision matrix)

I have a feeling that children perhaps inherits layerinformation from parent when copied…but i’m having a hard time figuring it out.

If these ramblings has made sense to anybody I would love to hear from you :wink:

Hi again

I fixed the problem using code, but that doesnt really change the fact that a problem exist. (Or the fact that I have misunderstood something :))
Still interested in hearing about possible solutions.

The code that solves my problem:

void OnCollisionEnter(Collision collision)
    {
        GameObject root = collision.gameObject.transform.root.gameObject;
        //Collectable newCollectable = (Collectable)collision.collider.gameObject.GetComponent(typeof(Collectable));
        Collectable newCollectable = (Collectable)root.GetComponent(typeof(Collectable));

        if (newCollectable != null)
        {
            addWeight(root.rigidbody.mass);
            newCollectable.DestroyMe();
        }
        
    }

I just got the root of the collider and then got the component from there

Hello,

I had the same problem and solved it by:

Remove the collider or trigger from the main object.
Create two empty objects and make them a child of the main object.
Bind to each child object a trigger or collider.
Size them to your needs.
Set the collision matrix appropriate for your prefab layer (ignore one of them).
Done!

Feature or bug? :face_with_spiral_eyes:

Peter.