Search code examples
c#unity-game-enginevirtual-reality

Cannot let go of an object when in another object's collider


I have some fairly simple code that allows me to grab, move, and let go of objects (in this case, the "LoopCubes" are the only things I want to be able to pick up:

public void grab(object sender, ClickedEventArgs e)
{
    if ((objectTouching != null) && objectTouching.CompareTag("LoopCube") == true)
    {
        objectTouching.gameObject.transform.SetParent(gameObject.transform);
    }
}

//Letting go of grip
public void releaseGrab(object sender, ClickedEventArgs e)
{
    if (objectTouching != null)
    {
        objectTouching.gameObject.transform.parent = null;
    }
}

This works as intended. However, a problem arises when I want to let go of the cube when the controller/cube are within a box collider (marked as a trigger). I can't let go and the cube becomes 'stuck' to the controller. What am I doing wrong?


Solution

  • Since I can't see your code I'm going to make some assumptions. This is what I'm guessing is happening:

    1. Enter a trigger and set it to objectTouching
    2. Grab objectTouching & move around
    3. Enter another trigger, which sets objectTouching to something else, still having the previous objectTouching grabbed
    4. Release, which tries to release the current objectTouching, which is the last trigger you entered - not the grabbed object.