- Home /
Picking Up Items (Inventory Implementation Problem)
I'm in the process of creating a basic inventory system for a game using Unity 2D, in which you can pick up items that act as triggers telling the InventoryController to use its addToInventory function, which searches for the first slot that does not have an item within it. However, I continuously have been getting the error: "Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption." I thought I had fixed this error by using Instantiate, but I'm still getting the exact same error. Help! Here is my addToInventory function. The problem points to the line after the comment.
The "slot" setup has to do with the UI implementation of the inventory. I used this tutorial as a guide, if that helps. You guys rock.
public void addToInventory (GameObject pickUp) {
foreach (Transform slotTransform in slots) {
GameObject slotItem = slotTransform.GetComponent<Slot>().item;
if (slotItem == null){
//The pickUp must be instantiated to have the parent set to the inventory slot
GameObject myPickUp = Instantiate(pickUp, new Vector2(pickUp.transform.position.x, pickUp.transform.position.y), Quaternion.identity) as GameObject;
myPickUp.transform.SetParent(slotTransform);
break;
}
}
}