Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Therian13 · Jun 11, 2014 at 09:50 AM · javascriptcrafting

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.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 3 · Share
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
avatar image Therian13 · Jun 12, 2014 at 12:58 AM 0
Share

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!

avatar image Therian13 · Jun 12, 2014 at 02:23 AM 0
Share

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...

avatar image Therian13 · Jun 12, 2014 at 07:30 AM 0
Share

never$$anonymous$$d. I fixed it. I just had to make it into a function and have it yield the second half.

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.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges