- Home /
Removing child component removes ALL components with that name
Hello Everyone,
I am currently trying to make a crafting script, but have run into a bit of an error. I am trying to get it that when I click a button that instantiates a new object (a campfire) , it will delete a child object (stick), but it wants to delete ALL inventory items named "stick" instead of just one. Would anyone know how I could fix this? I'm new to crafting scripts, so any help would be appreaciated.
Here is where if I "push button it creates a campfire" part of the script:
 var wood = 0;
 var ItemType : Transform;
 var ItemsInInv : Transform;
 var HideItem : Transform;
  var playersInv : Inventory;
function OnGUI {
 if(GUI.Button(Rect(100,60,100,20), "campfire"))
             {
                  if (wood >= 1)
                  {
                 clone = Instantiate(ItemType,transform.position, transform.rotation);
                 stone -= 15;
                  
                 for (var child : Transform in ItemsInInv.transform) 
                 {
                         if(child.name == "stick")
                         {
                         playersInv.RemoveItem(child);
                    
                     //Destroy(child.gameObject);
                    
                    }
                     }
     
                  }
     
         }
}
Here is the function where I am calling for the item to be removed in another script:
 function RemoveItem(Item:Transform)
 {
     var newContents=new Array(Contents);
     var index=0;
     var shouldend=false;
     for(var i:Transform in newContents) //Loop through the Items in the Inventory:
     {
         if(i == Item) //When a match is found, remove the Item.
         {
             newContents.RemoveAt(index);
             shouldend=true;
             //No need to continue running through the loop since we found our item.
         }
         index++;
         
         if(shouldend) //Exit the loop
         {
             Contents=newContents.ToBuiltin(Transform);
             if (DebugMode)
             {
                 Debug.Log(Item.name+" has been removed from inventroy");
             }
             if (playersInvDisplay != null)
             {
                 playersInvDisplay.UpdateInventoryList();
             }
             return;
         }
     }
 }
I know the script is a bit hectic, so I apologize in advance.
Answer by NoseKills · Jun 11, 2014 at 06:16 PM
You don't have a break; or a return; in the for-loop in the upper script so you indeed loop through all child objects that are called "stick".
Add break; on the next line after playersInv.RemoveItem(child); inside the for loop so you'll stop looking for sticks after you find one. 
thank you. That seemed to have fixed it. I also needed to tell it to destroy the child object, since it only hid the "stick" (in case anyone was copying my script) I appreciate your help on this.
 I can't stack items using this, but I'm sure I'll figure it out.
Thank you again Nose$$anonymous$$ills!
What if I wanted it to call 2 child objects of the same name? I've tried having the code repeat the same lines, but it doesn't work properly...
never$$anonymous$$d. I fixed it. I just had to make it into a function and have it yield the second half.
Your answer
 
 
             Follow this Question
Related Questions
Stopping a function when another function running. 2 Answers
Simple crafting system ; item pickup 1 Answer
Snow based questions 2 Answers
Spawn game objects on a timer 2 Answers
Talking Code 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                