pick a key inside a box

i want to pick up a key inside a box when its opened. i have already implemented the scripts for picking up normal keys and opening the box but when i combine those two and adding an if statement in key script (if box.IsOpen ==true) it doesnt work. IsOpen is a var in box script that changes when box opens and I have already included box gameobject in key script.

Expanding on @ThomasVandenberghe’s comment:

If you have an Interface:

interface PickupObject{
  void allowPickup();
  PickupObject pickup();
}

and a Box class

class Box{
  private PickupObject[] objectsInBox;
  public Box(PickupObject[] objectsInBox)
  {
    this.objectsInBox = objectsInBox;
  }

  public void Open()
  {
    foreach(PickupObject pickupObject in objectsInBox)
    {
      pickupObject.allowPickup();
    }
  }
}

Where the allowPickup() method will set the (private) boolean value on the PickupObject.

The player should never know whether or not the object is able to be picked up, they should just attempt to pick up the object (via pickup()) and let the PickupObject handle the rest. Either allowing the player to pick it up, or not allowing the player to pick it up.

You can easily implement the functionality you describe without the Key ever knowing where it is, and the box not ever caring what objects it contains.