Hello, everyone. I don’t want to copy myself but since that there was no answer for me in the “Answers” section I am hoping that you will help me here. Basicly I have created an inventory and an equipment system that is working (objects go from one window to another, stat changes, etc). The problem comes when I try to instantiate these gear objects on the player. It’s all good with the first one, but when I change it with another, the first one is still there and this way the player can have 1500 weapons on himself if he wants. Here is the part of the script responcible for these things.
What exactly do you mean by the first one is still there?? The stats are still there? The item is still in the item spot? The gameobject is still visible? It seems like you didn’t clean up your lists of things when you changed items, then recalculate the stats.
Hello and thank you for the comment! I mean that everything is fine (the stats are changing, the item icon is shown back in the inventory), but the problem is with the instantiated item. It is still on the player. I tried to destroy it, but the problem is I want to be able to call it again if the player wants to.
Yah I don’t think you need to destroy it. That doesn’t seem right. The work flow is the following below:
Item of type helmet in inventory, say in slot 1.
Player has helmet already on him
You now equip the helmet in the inventory to the player.
3.1. You need to move the item from inventory to player
3.2. You also need to move the item from player to inventory.
I believe you aren’t doing #5. You don’t want to destroy that item as that item goes back to your inventory. You just need to move it’s location. You instantiate things when you are moving them, you dont need to do that. Just move the existing gameobject/transform to the new location
BTW is your for loop even working?.. You seem to loop through it but never use i?
then you both seem to be confusing the object in the inventory and the gameobject in the scene with it’s associated renderer/mesh/etc. i.e. the data model and the display.
you do not need to destroy the item in the inventory, you do need to destroy the object in the scene that represents the object that has been equipped.
Wrong. Why would you destroy it to only Instantiate it again? That doesn’t make any sense. Just repurpose it - change the image as that’s all that is needed.
So you are proposing instead of destroying the object that is already on the player, just to send it away from the game terrain, but to be still in the scene? Thank you for the comments!
if you’re working 2d they’re proposing flipping the sprites image, if you’re working 3d they’re proposing you remap the renderers and mesh components and then in both cases you’d need to add/remove any associated scripts on the gameobject…
Thank you for the comment! I have fixed the code. Yes, I am talking about the 3D object that represents the 2D icon. You have to know that the most part of the code (about the items, icons, inventory window, equipment window) is not here because it will be too long and I think that we can fix it here. I will be very happy if you help me! If you need something, I am checking the forum every day :))
Ohhh… so the inventory and equipment UI is fine? but the actual armor on the player object is ‘stacking’ per say. As it looks like he is wearing multiple different shirts/armor pieces?
Is the above code the code that represents the actual gameobject and not the UI code?
The above code represents the changes that occur when the player clicks on an object from the inventory in order to equip it. And vise versa. Yes, it’s not about the UI.
Next let’s talk about where the real magic is happening. I’ll add comments into the code to make sure I understand what is happening and you can agree.
// Loop through all the equipment slots and if the item is equal to the right equipment spot do stuff
if(item.equipment_Type == equipment[0].slotEquipmentRect[i].equipType){
// We are checking if we are even allowed to wear this item
if(playerController.classType == item.require_Class || item.require_Class == ClassType.None)
{
// If the slot is currently empty, just put the item there. EZPZ. I'll leave the maintenance logic to you
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;
}
// If the slot was not empty
else {
// Create this item thing.
Item_Obj _item_obj = new Item_Obj();
// You just Instantiated an object, why are you doing this again? You are setting it to handler which you already did too
//handler = Instantiate(item.Gear, transform.position, transform.rotation);
//handler.transform.parent = transform;
// more maintenance stuff
_item_obj.item_id = slotEquipment_item[i].item_id;
_item_obj.euipmentType = slotEquipment_item[i].equipmentType;
_item_obj.item_amount = 1;
// This function ONLY, only, ONLY removes the stats (im assuming)
RemoveStatEquipmentToPlayer(Item_Data.instance.Get_Item(slotEquipment_item[i].item_id));
// Our slot is now holding the current stuff :)
slotEquipment_item[i].item_id = item.item_ID;
slotEquipment_item[i].equipmentType = item.equipment_Type;
// You add the stats to the player based on the item
AddStatEquipmentToPlayer(item);
// You are clearing out the bag spot and the Pickup_item code will put the switched out item in the inventory
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);
}
}
}
Your problem is two things:
You are Instantiating a new item to put on the player and assigning it to handler. Not sure what handler does or is.
Then you are Instantiating a new item again to put on the player and assigning it to handler. Bad.
You aren’t removing the previous item from the player. You need a Destroy() to get rid of the previous gameobject on the player. I don’t know how you store the gameobjects that the player is wearing.
To me it seems like you just attach the transform but you aren’t managing them? Is this script on every slot or it is more like a static function to handle things? Depending on how you are handling them, if the handler is the item currently on the exact slot you are equipping, then do a Destroy(handler) before you set it to the new item. If that makes sense.
Your problem is two things:
You are Instantiating a new item to put on the player and assigning it to handler. Not sure what handler does or is.
Then you are Instantiating a new item again to put on the player and assigning it to handler. Bad.
You aren’t removing the previous item from the player. You need a Destroy() to get rid of the previous gameobject on the player. I don’t know how you store the gameobjects that the player is wearing.
To me it seems like you just attach the transform but you aren’t managing them? Is this script on every slot or it is more like a static function to handle things? Depending on how you are handling them, if the handler is the item currently on the exact slot you are equipping, then do a Destroy(handler) before you set it to the new item. If that makes sense.[/QUOTE]
Hello! So the “handler” is an empty gameobject that represents the place where the weapon should instantiate. Every item in the game it’s saved with it’s properties (the equipment items have e gameobject field called “Gear” where I put the certain armor or weapon for the item icon). Maybe I am doing something wrong. Is there a function just to replace objects. Because the handler is only one and is something like replacing the old object with the new possible. Thank you!!
Got it. What I’m asking is how many of these “handler” do you have? Is there only one for every item or is there one for every slot? Is the script above on every single slot you have or is this script just on one object?
I’m thinking the following will fix your problem but not sure with the information I have.
Hello, I tried to use your code but maybe I dont understand where exactly to put it in mine. The handler is only one. I use its position to place gear. The differend item objects are “Gear”, so we need not to change the handler but to change the “Gear” object with the new one. Every item has it’s own gameobject field named “Gear”. Thank you!
What’s the point of the handler object then? How about this, can you post a picture of your gameobject hierarchy and have the object that has this script on it selected or highlighted? That way I can see the composition of your game and attempt to figure out what you are doing as you don’t seem to know honestly.
Hello, everyone! I have fixed the problem with these three lines of code -
var children = new List<GameObject>();
foreach (Transform child in transform) children.Add(child.gameObject);
children.ForEach(child => Destroy(child));
Thank you all for the comments! Have a great day! :))