- Home /
Question by
Nosmo · Jul 20, 2016 at 02:39 PM ·
arraysobjectssequenceorder-of-execution
How can i pick up object is an order
I'm trying to pick up a series of objects and after all of them have been picked up code is executed to open a door.
I've been using an array but its tricky.
Can anyone help?
Comment
There's a 1000 ways to do this. We need to see some code to see what might be wrong and how to make it work.
Answer by redhotspike · Jul 20, 2016 at 08:52 PM
If I understand your question you're trying to pick up objects in an order and once all the items have been picked up (in the correct order) then you want to open a door.
Assuming I understand correctly, how about something like the following:
public int TotalObjects; //there are more elegant ways to get this number but for this is just proof-of-concept
int _itemsPickedUp = 0;
bool _allItemsPickedUpInOrder = true;
public void PickUpObject(YourFancyGameObject obj) {
//in the object that you're picking up, have a public field that indicates what its order number is and compare it to the currently picked up items
//this assumes zero-index
if(obj.itemOrder != _itemsPickedUp) {
_allItemsPickedUpInOrder = false;
}
_itemsPickedUp++; //increment when you pick up an object
//if all items have been picked up
if(_itemsPickedUp == TotalObjects) {
//and all items were picked up in order
if(_allItemsPickedUpInOrder) {
OpenDoor(); //call the open door function
} else {
//if they weren't picked up in order
//display message to user or reset objects?
}
}
}
Answer by Vollmondum · Jul 20, 2016 at 08:51 PM
Easiest way is to create a tag, e.g. "doorKey", tag all those objects needed to be picked and run a simple script.
var doorKeys: GameObject[];
var openDoor: boolean;
function Update()
{
doorKeys = gameObject.FindGameObjectsWithTag("doorKey");
if(doorKeys.length == 0)
{
openDoor = true;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612085128im_/https://answers.unity.com/themes/thub/images/avi.jpg)