Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Collision detection problems on parented GameObject's

Discussion in 'Scripting' started by hooligan, Jan 8, 2011.

  1. hooligan

    hooligan

    Joined:
    Nov 26, 2010
    Posts:
    40
    Hey guys, just need a little help with collision detection between 2 GameObjects. I normally don't have a problem but I've come across a scenario that doesn't seem to work for me. Collision detection with an object inside of another objects hierarchy.

    All the creation is done at runtime. Firstly I create an empty GameObject. This acts as a container for the other GameObjects I Instantiate. The prefab I use has the following setup. Its a cube, with a box collider with is trigger set to true. I parent these GameObjects with the main container once they are created by using,

    Code (csharp):
    1. block.transform.parent = container.transform;
    I then have another external GameObject which is my hero. It is again a cube with a box collider and a rigid body. It also has a script attached to it which has the following simple OnTriggerEvent script.

    Code (csharp):
    1.  
    2. void OnTriggerEvent(Collider other) {
    3.     print("blah");
    4. }
    5.  
    Now I move the main container in my LevelManager script and watch for a collision in my scene window. I can see a collision happening but the on trigger event doesn't fire.

    The weird thing is if I add the Prefab without adding it to the Container GameObject and them play the OnTriggerEvent fires. When its inside of another GameObject it doesn't work?

    Am I missing something with the Collision detection with parented objects?

    All I'm trying to do is work out if an object outside the Container of GameObjects(hero) collides with one of the GameObject's inside the container.
     
    Last edited: Jan 8, 2011
  2. hooligan

    hooligan

    Joined:
    Nov 26, 2010
    Posts:
    40
    I still cant work this out?

    Can you get collisions from objects inside other game objects?
     
  3. hooligan

    hooligan

    Joined:
    Nov 26, 2010
    Posts:
    40
    Ok I was about to post I can't work it out but I just did.

    All I did was add a rigid body to the object inside the parent, am I right in saying that all moving objects need a rigid body applied for the collisions to happen. And if the object isn't moving it doesn't need a rigid body?

    I just thought that one of the two objects needed a rigid body? I didn't think it mattered which one.
     
  4. paulmac

    paulmac

    Joined:
    Feb 17, 2011
    Posts:
    10
    Ive had a similar problem whilst doing the walkerboys.com 2dspace shooter tutorial. the tutorial didnt mention adding rigidbodys to both the shield and the asteroid it was colliding with but I had to add a rigidbody to the shield in order for it to work. Is this something changed with a new update recently ?