• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RealGamesStudio · Feb 26, 2017 at 01:22 PM · instantiateobjectreplaceattachmentequipment

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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

Raycast Specific Object/Instantiate Explosion 1 Answer

How to make a recursion with created objects 2 Answers

Lock rotation of an instantiated object to another 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges