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 HuskyPanda213 · Jan 26, 2014 at 04:01 AM · c#buglist

Items not destroying.

Hello, I just almost got everything in a crafting system complete :). I just have one more bug, when I craft something the items used do not delete! How do I fix this, what am I doing wrong!

Code:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 
 public class PlayerInventory : MonoBehaviour {
 
     public List<Item> MyItems = new List<Item>();
 
     public Weapon weapon1;
     public Weapon weapon2;
 
     public Clothes MyClothes;
 
     public PlayerWeaponManager pwm;
     public PlayerClothesManager pcm;
 
     public void AddWeapon(Weapon we){
         if(networkView.isMine){
             if(we.weslot == 1){
                 weapon1 = we;
             }
             if(we.weslot == 2){
                 weapon2 = we;
             }
             pwm.SwitchWeapon(we.weslot);
         }
 
     }
 
     void OnGUI(){
         if(GUILayout.Button("Add wood")){
             MyItems.Add(Item.Wood());
         }
         if(GUILayout.Button("Craft wood-wall")){
             Craft(Craftable.WoodenWall());
         }
     }
 
     public void AddClothes(Clothes _clothes){
         MyClothes = _clothes;
     }
 
 
 
     public void AddItem(Item added){
         if(networkView.isMine){
             MyItems.Add (added);
         }
     }
 
 
     /*
      * 
      * Basic Crafting!
      * 
      */
 
 
     public void Craft(Craftable recipe){
         bool b = CheckItems (recipe.InputItems);
         if (b) {
             foreach(Item it in recipe.InputItems){
                 MyItems.Remove(it);
             }
             MyItems.Add(recipe.output);
         }
         else{
             Debug.Log("Not enough material!");
         }
     }
 
     /*public bool CheckIfItemsInInventory(List<Item> check){
         return !check.Except(MyItems).Any();
     }*/
     public bool CheckItems(List<Item> check) {
         foreach(Item inventoryItem in MyItems) {
             if (check.Contains(inventoryItem)) {
                 check.Remove(inventoryItem);
                 if(check.Count == 0) {
                     return true;
                 }
             }
         }
         return false;
     }
 
 
     //This crafting system is not used anymore.
 
     /*public void Craft(Craftable recipe){
         Item _1 = new Item();
         Item _2 = new Item();
         Item _3 = new Item();
         if (CheckForItemsByName(myItems,recipe.input1.itname,out _1)) {
             if(CheckForItemsByName(myItems,recipe.input2.itname,out _2)){
                 if(recipe.input3.itname.Length > 1){
                     if(CheckForItemsByName(myItems,recipe.input3.itname,out _3)){
                     myItems.Add(recipe.output);
                     myItems.Remove(_1);
                     myItems.Remove(_2);
                     myItems.Remove(_3);
                     return;
                 }
                 }
                 else{
                     myItems.Add(recipe.output);
                     myItems.Remove(_1);
                     myItems.Remove(_2);
                     return;
                 }
             }
         }
         else{
             Debug.Log("Not enough material!");
         }
     }*/
 
     /*public bool CheckForItemsByName(List<Item> _list, string lookfor, out Item found)
     {
         foreach (Item it in _list) {
             if(it.itname == lookfor){
                 found = it;
                 return true;
             }
         }
         found = null;
         return false;
     }*/
 
 }

 using System.Collections;
 
 [System.Serializable]
 public class Item {
 
     public string itname;
     public string ittype;
     public float itvalue;
     public float itdur;
 
     public override bool Equals(object other) {
         if (other.GetType() != typeof(Item)) return false;
         return ((Item)other).itname == this.itname;
     }
 
     public static Item Wood(){
         Item i = new Item ();
         i.itname = "Wood";
         i.ittype = "Craft";
         i.itvalue = 1;
         i.itdur = 1;
         return i;
     }
 
     public static Item WoodenWall(){
         Item i = new Item ();
         i.itname = "Wooden Wall";
         i.ittype = "Build";
         i.itvalue = 1;
         i.itdur = 1;
         return i;
     }
 
 }
Comment
Add comment · Show 4
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 Invertex · Jan 26, 2014 at 04:36 AM 0
Share

I don't see any DestroyImmediate() lines.. How exactly are you trying to destroy items?

avatar image Aladine · Jan 26, 2014 at 04:49 AM 0
Share

what do you mean "Destroying" ??? if you want to destroy a game object (remove it from scene) then use Destroy(objectName) otherwise you need explain your problem a little bit more

avatar image Invertex · Jan 26, 2014 at 05:15 AM 0
Share

@Aladine , Don't always use Destroy() though, if you have functions happening later in your script that rely on knowing if an object exists or not, then you want to use DestroyImmediate(object,true);, so that it's references are set to null immediately, not after the frame is finished.

avatar image Aladine · Jan 26, 2014 at 05:20 AM 0
Share

@Invertex , Yes i know that but it's a bit "complicated" subject to talk about in this topic and since he want to destory, Destroy() is the first function i thought about, thanx anyway ^^

1 Reply

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

Answer by oatsbarley · Jan 26, 2014 at 10:41 AM

The problem is caused by CheckItems() removing items from the list passed to it. Because it gets passed a reference to the list, any changes made will affect recipe.InputItems. So by the time you get to this section:

 foreach(Item it in recipe.InputItems) {
       MyItems.Remove(it);
 }
 MyItems.Add(recipe.output);

recipe.InputItems is already empty and so nothing is removed from MyItems.

Inside CheckItems() you need to make a copy of the list passed to it like so:

 var copy = new List<Item>(check);

And then use the copy inside the foreach loop instead of the original.

Comment
Add comment · 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

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

21 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

A node in a childnode? 1 Answer

Pathfinding through pairs of connections 2 Answers

generic list, swapping equipment errors but not always? 1 Answer


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