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.
I don't have a specific answer for you, just some advice.
It doesn't sound right, from a design perspective, for there to be a box object on the key. The key shouldn't care where it is, it just cares about being able to be picked up and actually being picked up.
The box may control some of the properties of the key (being able to be picked up) but the key shouldn't care at all where it is contained. Whether its in a box, a secret compartment, buried in dirt, etc.
yeah but how can I implement it so it can be picked only if the box is opened? I have one box collider for box so player first has to open it when triggered and then a second collider for the key and player can pick it up after he opens the box. that's why I have the box object on the key. If i dont have it, then by pressing E, it would both open box and pick up key at the same time :/
Something along the lines of the key inheriting from an PickupObject which has a boolean (CanPickUp), the box can then contain a list of different pickupobjects which have the boolean CanPickUp to false. Once the box is opened you can set the list of objects to CanPickUp = true (with or without taking into account the animation delay for opening the box)
Having the key inheriting from a pickupobject would enable the box to store different objects as well without much coding.
I can't tell you for certain without getting more information, but it sounds to me like you're drastically overcomplicating what should be a fairly simple problem. In most cases that I can think of the box and key won't even need to be aware of each other, but I need to know more to really help. How are you detecting if the player can interact with the box? Raycasts or just simple proximity? What is the camera perspective, top-down, first person, etc.? The scripts for the box and key would be helpful as well.
its first person. I have one box collider for box so player first has to open it when triggered and then a second collider for the key and player can pick it up after he opens the box. in box script if player triggered the collider and press E the box opens and the var IsOpen changes to true. And then in key script I have if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E) && playerNextTo$$anonymous$$ey == true && box.IsOpen==true) { The$$anonymous$$ey.active = false; }
shouldn't it be "The$$anonymous$$ey.SetActive(false)"
Answer by Brocccoli · Mar 18, 2016 at 04:46 PM
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.