- Home /
REPLACING INSTANTIATED OBJECT
Hello, everyone! I've been trying to find a solution for my problem for a while and I can't manage to do so. I am trying to create a mechanic in which the equipment of the player changes via a function. Everything is working - the item goes from the inventory to the equip tab, it changes player stats and the items can be replaced, but that's not the case with the objects (weapons) I am trying to spawn. The first use is fine because it spawns perfectly, but I cannot find a way for the upcoming change of equipment just to replace the old one. And right now they can be 300 weapons at the same time and that's not cool. Here is the part of the code where this happens -
public GameObject handler;
public void Use_Item(int _itemID){
Item_Data.Item item = Item_Data.instance.Get_Item(_itemID);
if(item != null){
if(item.equipment_Type == Item_Data.Equipment_Type.Null && item.gold == false){
if(item.potion == true){
AddEffectItemToPlayer(item);
bag_item[indexInventory].item_amount -= 1;
if(bag_item[indexInventory].item_amount <= 0){
bag_item[indexInventory].item_id = 0;
bag_item[indexInventory].item_amount = 0;
bag_item[indexInventory].equipmentType = Item_Data.Equipment_Type.Null;
}
}
}else if(item.equipment_Type != Item_Data.Equipment_Type.Null){
Debug.Log("Equipment");
if (item.Gear != null)
{
handler = Instantiate(item.Gear, transform.position , transform.rotation );
handler.transform.parent = transform;
}
for (int i = 0; i < equipment[0].slotEquipmentRect.Length; i++){
if(item.equipment_Type == equipment[0].slotEquipmentRect[i].equipType){
if(playerController.classType == item.require_Class || item.require_Class == ClassType.None)
{
if(slotEquipment_item[i].item_id == 0){
AddStatEquipmentToPlayer(item);
slotEquipment_item[i].item_id = item.item_ID;
slotEquipment_item[i].equipmentType = item.equipment_Type;
bag_item[indexInventory].item_id = 0;
bag_item[indexInventory].item_amount = 0;
bag_item[indexInventory].equipmentType = Item_Data.Equipment_Type.Null;
bag_item[indexInventory].linkIndex = -1;
}else{
item.Gear.gameObject.SetActive(false);
Item_Obj _item_obj = new Item_Obj();
handler = Instantiate(item.Gear, transform.position, transform.rotation);
handler.transform.parent = transform;
_item_obj.item_id = slotEquipment_item[i].item_id;
_item_obj.euipmentType = slotEquipment_item[i].equipmentType;
_item_obj.item_amount = 1;
RemoveStatEquipmentToPlayer(Item_Data.instance.Get_Item(slotEquipment_item[i].item_id));
slotEquipment_item[i].item_id = item.item_ID;
slotEquipment_item[i].equipmentType = item.equipment_Type;
AddStatEquipmentToPlayer(item);
bag_item[indexInventory].item_id = 0;
bag_item[indexInventory].item_amount = 0;
bag_item[indexInventory].equipmentType = Item_Data.Equipment_Type.Null;
bag_item[indexInventory].linkIndex = -1;
Pickup_Item(_item_obj);
}
}
}
}
}
}
I will be REALLY thankful if you help me with this guys! Thank you!
Your answer
Follow this Question
Related Questions
How can I replace an object while keeping the references to that object? 2 Answers
Instantiate and destroy on given location 1 Answer
Referencing a variable in another script possibly not working? 0 Answers
How to detect if object is within another object? 0 Answers
Reference by Instance ID? 2 Answers