Search Unity

Communicating between prefab and collider

Discussion in 'Scripting' started by methinks, Jun 27, 2021.

  1. methinks

    methinks

    Joined:
    May 13, 2013
    Posts:
    7
    I have a game object (A) that contains an instanced prefab containing colliders. I need this object to be notified when the collider touches game object B.

    The preferred solution would be for object A to query the collider directly, something like calling prefab.collider.OnCollide, but I haven't found any way to do this.

    The next best solution is for object B to catch the collision, and report it back to object A. I can use object B's OnCollisionEnter(Collision) to catch the collision and use the Collision parameter to find Object A's collider, but if I try to use parent it only takes me back to the prefab, not all the way to object A.

    The only solution I see is to modify or replace the prefab with something custom, which I would rather not do.

    Does anybody have any other solutions?

    Just some details in case it helps: I'm using SteamVR and I would like to get collisions for individual fingers rather than the generic actions. The prefab that I'm trying to access is HandCollider in the HandPhysics script.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Can you take a screenshot of the hierarchy with all the objects expanded, and explain which objects are 'A', 'the prefab', and 'B'?

    OnCollisionEnter and navigating the Transform hierarchy should easily get you what you want.
     
  3. methinks

    methinks

    Joined:
    May 13, 2013
    Posts:
    7
    Here it is:
    upload_2021-6-27_17-10-26.png
    Object A: SampleScene->Player->SteamVRObjects->BodyCollider->LeftHand
    Object B: SampleScene->Block (Default cube with collider and RigidBody)
    Prefab: The component "Hand Physics"->"Hand Collider Prefab"

    upload_2021-6-27_17-13-3.png
    This is a view of the prefab. "HandColliderLeft" has a Rigidbody and a script attached to it, everything else are colliders. I'd like to monitor "thumb_distal" and "finger_index_2_r" for collisions.

    Everything that is part of the Player object is a stock import from the SteamVR package.

    Thanks
     
  4. methinks

    methinks

    Joined:
    May 13, 2013
    Posts:
    7
    Also, I have tried to navigate the transform Hierarchy. I can traverse from the collider to the prefab, but I can't figure out how to go from the prefab to the parent. Again, the prefab is instantiated in code, not in the editor, so it doesn't find anything if I just try to use .parent.
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    From your screenshots its doesn't appear as though you have actually instantiated the prefab at all. You just have a prefab reference on your script.

    For the prefab to appear in the game world your code needs to call Instantiate on the Prefab. Have you done this? If so, can you take screenshots again, but this time while the game is running after the prefab was instantiated?
     
  6. methinks

    methinks

    Joined:
    May 13, 2013
    Posts:
    7
    The prefab is instantiated in the script (HandPhysics) that is part of the imported package.
    The instantiated copy of the prefab was hidden using [HideInInspector] which I commented out for the screenshot:
    upload_2021-6-27_18-29-41.png
     
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    The interesting thing would be where is that clone in the hierarchy when it's instantiated? That would determine how to get to Object A from it at runtime.
     
  8. methinks

    methinks

    Joined:
    May 13, 2013
    Posts:
    7
    It looks like it is a child of the Player object.
    upload_2021-6-27_19-17-19.png