Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by Kamil1064 · Mar 29, 2016 at 08:40 PM · classinventorydictionaryitemitems

int variable from dictionary?

Hi, i have simple inventory dictionary, here is the script:

 public class Items
     {
         public string name;
         public int itemID;
         public int quantity;
     }
 Dictionary<int, Items> inventory = new Dictionary<int, Items>();

Later I set up some items which working but I would like to set an int from an item quantity from other script:

 public Inventory myInventory;
 public int countInt;

 void start(){
    countInt = myInventory[0].quantity // I don't know how to get it :(
 }


Comment
Add comment · Show 13
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 TreyH · Mar 29, 2016 at 08:45 PM 1
Share

What is your question? Does that work?

avatar image gjf · Mar 29, 2016 at 08:46 PM 0
Share

the type for myInventory should be the same as the definition of your Dictionaryand you'll need to make sure that it's referring to the same thing...

also, unity won't call start() ... try Start() ;)

avatar image Kamil1064 gjf · Mar 29, 2016 at 08:54 PM 0
Share

Of course it's Start, my mistake, correct in script anyway :) I want my countInt value from Inventory script an Item quantity, for example from quantity of first item in inventory which I know I have 3, so I want my countInt to be 3.

avatar image TreyH · Mar 29, 2016 at 08:58 PM 1
Share

Where are you assigning it to be 3?

avatar image Kamil1064 TreyH · Mar 29, 2016 at 09:10 PM 0
Share

In an gameobject I have attached script where is countInt variable (second on post above). On my player I have got inventory script (first on post above). What I want is my countInt depends on first item quantity.

avatar image TreyH Kamil1064 · Mar 29, 2016 at 09:14 PM 0
Share

Do you ever call (pseudocode) player.inventory[0].quantity = 3; to assign that?

Classes are all referenced, changes made to an object in one script are reflected in another.

Show more comments
avatar image TreyH · Mar 29, 2016 at 09:51 PM 1
Share

First, we'll make the Items class:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Items
 {
     public string name;
     public int itemID;
     public int quantity;
 
     // Instantiate
     public Items(string _name, int _id, int _quantity)
     {
         this.name = _name;
         this.itemID = _id;
         this.quantity = _quantity;
     }
 
     // Sanity Check
     public void Describe()
     {
         Debug.LogFormat("Item: {0}, ID: {1}, Quantity: {2}", this.name, this.itemID, this.quantity);
     }
 }

$$anonymous$$eeping your dictionary format, we'll make a simple Player class and attach this to some cube in scene.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Player : $$anonymous$$onoBehaviour {
     // Player's Inventory
     public Dictionary<int, Items> inventory = new Dictionary<int, Items>();
 
     // ID and dictionary index will be the same in this example
     public const int POTION_ID = 0;
 
     // Use this for initialization
     void Start () {
         // Create an item
         Items potion = new Items("Potion", POTION_ID, 3);
 
         // Print how many of an  item we start with
         inventory.Add(POTION_ID, potion);
     }
     
     // Update is called once per frame
     void Update () {
 
         // Print this quantity whenever you press a key
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
         {
             AddPotions(3);
         }
     }
 
     // Add item to inventory
     void AddPotions(int newCount)
     {
         // Check how many we have
         Debug.Log ("Previous Potions:");
         inventory[POTION_ID].Describe();
 
         // Add the number of potions, then  describe the potion  instance
         inventory[POTION_ID].quantity += newCount;
 
         // Check the update
         Debug.Log ("Current Potions:");
         inventory[POTION_ID].Describe();
     }
 }

Giving us the desired result after pressing Spacebar:

alt text

potionexample.png (155.8 kB)
avatar image TreyH TreyH · Mar 29, 2016 at 11:53 PM 0
Share

@$$anonymous$$amil1064 does this work?

avatar image Kamil1064 · Mar 30, 2016 at 09:13 AM 0
Share

Sorry @TreyH, I think I should at start post everythong I have :) So here it is:

     using UnityEngine;
     using System.Collections;
     using System.Collections.Generic;
 
 public class Inventory : $$anonymous$$onoBehaviour {
 
     public class Items
     {
         public string name;
         public int itemID;
         public int quantity;
     }
     Dictionary<int, Items> inventory = new Dictionary<int, Items>();

 void Start () { // add first item
         Items myItem = new Items();
         wytrych.name = "first";
         wytrych.itemID = 0;
         wytrych.quantity = 3;
         inventory.Add(k, myItem);
         k++;
     }
 public bool IsItemExists(int itemID) // if exists return true
     {
         return inventory.Contains$$anonymous$$ey(itemID);
     }
 public void AddItem(int idFor$$anonymous$$ey)
     {
         if(inventory.Contains$$anonymous$$ey(idFor$$anonymous$$ey))
             inventory[idFor$$anonymous$$ey].quantity ++;
         else
         {
             Items newItem = new Items();
             newItem.name = "item_" + idFor$$anonymous$$ey;
             newItem.itemID = idFor$$anonymous$$ey;
             newItem.quantity ++;
             k++;
             inventory.Add(idFor$$anonymous$$ey, newItem);
             print("nie mam, dodaje nowe");
         }
     }
 
     public void RemoveItem(int keyIds)
     {
         if(inventory[keyIds].quantity > 1)
             inventory[keyIds].quantity --;
         else
             inventory.Remove(keyIds);
     }
 }

And another script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;

 public class otherScript : $$anonymous$$onoBehaviour {

 public Inventory myInventory;
 private int countInt;
 public int myID;
 void Start()
 {
 countInt = // here i don't know how to get first item quantity from inventory, which I know is 3
 }
 public void Check()
 {
 if(door_inven.IsItemExists(myID))
 {
 //do some stuff, working fine
 }
 }
 public void RemoveItem()
 {
 myInventory.RemoveItem(myID);//works fine
 }
 public void AddItem()
 {
 myInventory.AddItem(myID);//works fine
 }
 }



avatar image TreyH Kamil1064 · Mar 30, 2016 at 02:42 PM 0
Share

Well,

 countInt = myInventory[FIRST_ITE$$anonymous$$_ID].quantity;

is how you would do it, but again you need to assign that somewhere else. $$anonymous$$y example shows how to do this, have you tried to implement it?

avatar image Kamil1064 TreyH · Mar 30, 2016 at 03:35 PM 0
Share

I get error when I'm trying to do it with my version

 error CS0021: Cannot apply indexing with [] to an expression of type `Inventory'

I check it with yours and it works, however i get warning

 warning CS0108: `Items.name' hides inherited member `UnityEngine.Object.name'. Use the new keyword if hiding was intended


But I can now set countInt so it looks like it's better now :) Now I just need get deep into this and make some functions, thanks :) Edit: I cannot reply below so I'm writing here: Thanks @TreyH, everything is working now, I even wrote remove quantity/item and it's also working. Of course countInt = ... also working fine :)

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

39 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

Related Questions

List check argument of item 3 Answers

How do I let the player pick up an item with left click? 0 Answers

Add Item to inventory - Shop UI intro and setup 0 Answers

How can I spawn an item after killing a wave of 6 enemies?,How can I spawn an item after clearing a wave of 6 enemies? 0 Answers

Inventory and item class design 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