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 WarLordYT · Nov 02, 2013 at 04:54 AM · listclassinventoryadd

[CLOSED] When I use this code that I made, it adds to the item catalogue too? [CLOSED]

This code that I programmed is the begginings of an inventory system I'm making, and the Item Catalogue will contain every attainable item in the game, anyway this inventory AddItem is supposed to find out if theres already an item with this name, if there isn't, add the item with its values to the inventory, but if theres already an item with the name, then add to the quantity, however, when I add to "ItemToAddTo" it also adds to the ItemCatalogue? And I can't for the life of me figure out why? (The Item Catalogue script is just a list, nothing else, and the itemclass which is where the Items' information is stored is just a class with no actual code besides a bunch of variables in it.

 import System.Collections.Generic;
 
 var ItemCatalogue;
 var Inventory : List.<ItemClass> = new List.<ItemClass>();
 
 var Weight : float;
 
 
 
 function Start()
 {
     ItemCatalogue = GameObject.FindWithTag("GameLogic").GetComponent("Item Catalogue").Catalogue;
     
     RecalWeight();
 }
 
 function Update()
 {
 
 }
 
 function RecalWeight()
 {
     Weight = 0;
     
     for each(var InventoryItem in Inventory)
     {
         Weight += InventoryItem.Weight * InventoryItem.Quantity;
     }
 }
 
 function AddItem(Item : ItemClass)
 {
     for each(var InventoryItem in Inventory)
     {
         if(InventoryItem.Name == Item.Name)
         {
             var IsInInventory = true;
             var ItemToAddTo = InventoryItem;
         }
     }
     
     if(IsInInventory)
     {
         ItemToAddTo.Quantity += Item.Quantity;
     }
     else
     {
         Inventory.Add(Item);
     }
     
     IsInInventory = false;
     
     RecalWeight();
 }
 
 function OnGUI()
 {
     if(GUI.Button(Rect(10, 70, 50, 30), "Items"))
     {
         AddItem(ItemCatalogue[0]);
     }
 }



Any ideas?

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 Fornoreason1000 · Nov 02, 2013 at 06:20 AM

Its because your adding the item on its self, you need to create a new instance of the item to add it. your setting up a reference to the item catalog thus when you add the quantity, it duplicates. use the instantiate command and it will crate a new instance of the item out of the catalog .

 import System.Collections.Generic;
  
 
 var Inventory : List.<ItemClass> = new List.<ItemClass>();
 var ItemCatalogue;
 var Weight : float;
  
  
  
 function Start()
 {
     ItemCatalogue = gameObject.GetComponent("ItemList").Catalogue;
  
     RecalWeight();
 }
  
 function Update()
 {
  
 }
  
 function RecalWeight()
 {
     Weight = 0;
  
     for each(var InventoryItem in Inventory)
     {
        Weight += InventoryItem.Weight * InventoryItem.Quantity;
     }
 }
  
 function AddItem(Item : ItemClass)
     {
         for each(var InventoryItem in Inventory)
         {
            if(InventoryItem.Name == Item.Name)
         {
              var IsInInventory = true;
              var ItemToAddTo = InventoryItem;
         }
 }
  
 if(IsInInventory)
 {
     ItemToAddTo.Quantity += Item.Quantity;
 }
 else
 {
     Inventory.Add(Item);
 }
  
 IsInInventory = false;
  
 RecalWeight();
 }
  
 function OnGUI()
 {
     if(GUI.Button(Rect(10, 70, 50, 30), "Items"))
     {
         AddItem(Instantiate(ItemCatalogue[0]));
     }
 }

Interesting structure you have, less of a head ache that database every item and using its id to determine quantity. hope it helps

Comment
Add comment · Show 1 · 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 WarLordYT · Nov 02, 2013 at 06:49 AM 0
Share

YES!!! I looked up what you said and found that you need to create a new class, and assign the values, thanks! :D

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

16 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

Related Questions

A node in a childnode? 1 Answer

Problem with script 1 Answer

Lists, Is a type but is used like a variable Problem C# 1 Answer

List of Objects in Boo 0 Answers

How can i store the Amount of an item i have in an inventory? 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