Search Unity

Change Collider Prefab

Discussion in 'Prefabs' started by SickBoyRulez, Jul 8, 2021.

  1. SickBoyRulez

    SickBoyRulez

    Joined:
    Mar 30, 2021
    Posts:
    3
    Hi developers,
    I'm editing an existing script of HTC Vive VR system, precisely the part concerning the controllers/hands.
    The hands do not have a collider assigned, but there is a script that provides for it, the collider with the rigidbody is instantiated at start by a prefab.
    I would like to add a line of code that changes this prefab when something happens (for example if I take an object I would like the object to become one with the hand, becoming the new collider) is it possible?
     
  2. pietrodenicola

    pietrodenicola

    Unity Technologies

    Joined:
    Dec 8, 2020
    Posts:
    43
    I assume that you don't want to change the prefab (that is in your asset folder) but the instance of the prefab (that is alive in the scene). And you want to change parent of a GameObject.

    Let's say that you have a key in the drawer. The parent of the GameObject key should be the drawer GameObject, so that it moves with the drawer (and another way to say it is: the key GameObject is a child of the drawer GameObject).

    Now, the player touches the key (collision detected), you want the key to stick with the hand of the player (and no longer with the drawer).

    Some reparenting has to happen. Something like:
    Code (CSharp):
    1. key.transform.SetParent(hand.transform);
    Check the documentation to see how to use SetParent:
    https://docs.unity3d.com/ScriptReference/Transform.SetParent.html
     
    Last edited: Jul 13, 2021