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 ramunas946 · Aug 13, 2021 at 04:25 PM · crafting

how to compere recipes and inventory for crafting?

  public class CraftingItem : MonoBehaviour
 {
     public string Name;
     public Sprite Image;
     public List<InventorySlot> inventory;
     public List<Recepie> recepie;
     public List<int> toDeleteindex;
     public bool isInInventory = false;
     public bool CanCraft = false;
     public int recepiecheck;
 
 
     public void Start()
     {
 
         gameObject.transform.parent.transform.GetChild(1).GetComponent<Text>().text = Name;
         gameObject.transform.parent.transform.GetChild(0).GetComponent<Image>().sprite = Image;
     }
     public void CraftItem()
     {
         inventory = GameObject.Find("Player").GetComponent<Inventory>().inventorySlot;
         GameObject.Find("Player").GetComponent<Inventory>().UpdateInventory();
 
         for (int i = 0; i < recepie.Count; i++)
         {
             for (int j = 0; j < inventory.Count; j++)
             {
                 {
                     if (recepie[i].Name == inventory[j].Name)
                     {
                         if (recepie[i].Amount <= inventory[j].Amount)
                         {
                             toDeleteindex.Add(j);
                             recepiecheck +=1;
                         }
                         else
                         {
                             if (!toDeleteindex.Contains(j))
                             {
                                 recepiecheck -=1;
                                 return;
                             }
                         }
                     }
                 }
 
             }
         }
         Debug.Log(recepiecheck);
         if(recepiecheck == recepie.Count)
         {
             CanCraft = true;
         }
         else
         {
             CanCraft = false;
         }
         CraftItemitem();
         CanCraft = false;
         recepiecheck = 0;
         GameObject.Find("Player").GetComponent<Inventory>().UpdateInventory();
     }
 
     public void CraftItemitem()
     {
         if (CanCraft == true)
         {
             if (isInInventory == true)
             {
 
                 for (int i = 0; i < inventory.Count; i++)
                 {
                     if (inventory[i].Name == Name)
                     {
                         inventory[i].Amount += 1;
                     }
                 }
                 for (int i = 0; i < toDeleteindex.Count; i++)
                 {
                     inventory[toDeleteindex[i]].Amount -= recepie[i].Amount;
                 }
                 toDeleteindex.Clear();
 
             }
             else
             {
                 for (int i = 0; i < toDeleteindex.Count; i++)
                 {
                     inventory[toDeleteindex[i]].Amount -= recepie[i].Amount;
                 }
                 inventory.Add(new InventorySlot(Name, 1, Image));
                 isInInventory = true;
                 toDeleteindex.Clear();
             }
         }
     }
 }
 
 
 
 
Comment
Add comment · Show 2
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 GamerLordMat · Aug 14, 2021 at 09:51 AM 0
Share

Could you please define what exactly problem is?

avatar image ramunas946 GamerLordMat · Aug 14, 2021 at 10:13 AM 0
Share

crafting works only if i have items in inventory that match recepie if i try to craft without items needed to craft it break all other crafting when item isn't crafted and i try to craft something else it just dos not work erros: ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.

1 Reply

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

Answer by ramunas946 · Aug 14, 2021 at 11:15 AM

i think i fixed it

  public class CraftingItem : MonoBehaviour
 {
 public string Name;
 public Sprite Image;
 public List<InventorySlot> inventory;
 public List<Recepie> recepie;
 public List<int> toDeleteindex;
 public bool isInInventory = false;
 public bool CanCraft = false;
 public int recepiecheck;


 public void Start()
 {

     gameObject.transform.parent.transform.GetChild(1).GetComponent<Text>().text = Name;
     gameObject.transform.parent.transform.GetChild(0).GetComponent<Image>().sprite = Image;
 }
 public void CraftItem()
 {
     inventory = GameObject.Find("Player").GetComponent<Inventory>().inventorySlot;

     for (int i = 0; i < recepie.Count; i++)
     {
         for (int j = 0; j < inventory.Count; j++)
         {
             {
                 if (recepie[i].Name == inventory[j].Name)
                 {
                     if (recepie[i].Amount <= inventory[j].Amount)
                     {
                         toDeleteindex.Add(j);
                         recepiecheck += 1;
                     }
                     else
                     {
                         if (!toDeleteindex.Contains(j))
                         {
                             recepiecheck -= 1;
                             return;
                         }
                     }
                 }
             }

         }
     }
     for (int i = 0; i < toDeleteindex.Count; i++)
     {
         Debug.Log(toDeleteindex[i]);
     }

     if (recepiecheck == recepie.Count)
     {
         CanCraft = true;
     }
     else
     {
         CanCraft = false;
         toDeleteindex.Clear();
     }
     CraftItemitem();
     CanCraft = false;
     recepiecheck = 0;
     GameObject.Find("Player").GetComponent<Inventory>().UpdateInventory();
 }

 public void CraftItemitem()
 {
     var firstitem = true;
     if (CanCraft == true)
     {
         for (int i = 0; i < inventory.Count; i++)
         {
             if (inventory[i].Name == Name)
             {
                 inventory[i].Amount += 1;

                 firstitem = false;
                 break;
             }
         }
         if (firstitem == true)
         {
             inventory.Add(new InventorySlot(Name, 1, Image));
         }
         for (int i = 0; i < toDeleteindex.Count; i++)
         {
             inventory[toDeleteindex[i]].Amount -= recepie[i].Amount;
         }
         toDeleteindex.Clear();
     }
 }

}

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

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

Crafting system 2 Answers

Problem at crafting 1 Answer

Simple Crafting and Inventory System 0 Answers

Crafting system NOT like minecraft 1 Answer

How can you make an inventory and crafting system? 0 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