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 deadmansfrag · Dec 22, 2013 at 10:01 PM · c#listinventory systemserialize

Calling for two variables in a list

What would be the best way to get 2 variables from a list item. I have an inventory system where one item has multiple variables , and the two I am trying to access are ItemName which is a string and then IsEqquipped which is a bool . So far everything I have done has only needed to call on one variable at a time from the item in the list .Now save what inventory the player has equipped i need to call two variables at once from the item.

Comment
Add comment · Show 8
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 Sundar · Dec 23, 2013 at 02:58 AM 0
Share

If you are using JS use hash table http://docs.unity3d.com/Documentation/ScriptReference/Hashtable.html

If you are using c# use dictionary

avatar image Statement · Dec 23, 2013 at 03:00 AM 0
Share

(C#, judging by the tags)

avatar image KellyThomas · Dec 23, 2013 at 03:32 AM 0
Share

If your data becomes more complex you may want to look at creating a Class or Struct.

avatar image deadmansfrag · Dec 23, 2013 at 03:58 AM 0
Share

I have a custom class for the items.Right now I am just using a simple method of getting all the items in the players inventory by name and serializing it. but ive come to the point where i need to save if items in the players inventory are equipped or not and save that bool with the corresponding item. Heres some of the code i have so far

Item Class : using UnityEngine; using System.Collections;

 [System.Serializable]
 
 public class Items :ScriptableObject{
 
     public int cost = 0;
     
     public string ItemName = "";
 
     public string ItemType = "";
     
     public string description = "";
     
     public Texture2D Icon;
 
     public GameObject ItemPrefab;
 
     public int Id = 0 ;
 
     public bool IsEquipped = false ;



 
avatar image deadmansfrag · Dec 23, 2013 at 04:00 AM 0
Share

Saving player inventory through serialize when items are added from the market :

 private void Serialize <T>(List<Items> list)
         
     {
         
         XmlSerializer serializer = new XmlSerializer(typeof(List<T>));
         
         TextWriter tw = new StreamWriter("PlayerInv.txt");
         
         serializer.Serialize(tw, list);
         
         tw.Close();


 public void PurchaseCon ( int windowID){
 
         if (GUI.Button(new Rect(Screen.width*ButtonPlacementX3,Screen.height*ButtonPlacementY3,Screen.width*ButtonWidth2,Screen.height*ButtonHeight2),"Confirn"))
         {
             for(int i = 0; i < cart.Count;i++)
             {
             GameObject findplayer = GameObject.FindWithTag("Player");
             pl = findplayer.GetComponent<PlayerInventory>();
             pl.playerinv.Add(cart[i]);
             pl.playerinvname.Add(cart[i].ItemName);
             cart.Clear();
             purchasecon = false;
             
                 List<Items> playerlist = pl.playerinvname;
 
                 Serialize<string>(playerlist);
 
                     }    
         }
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Sundar · Dec 23, 2013 at 03:05 PM

Here you go

 using System;
 using System.Collections.Generic;
 
 // here string is the "name" of the item and bool is "Is equipped"
 Dictionary<string, bool> items = new Dictionary<string, bool>();
 
 // add items 
 
 items.add( "item1", true );// item1 is equipped
 items.add( "item2", false )// item2 is  not equipped
 
 // find item equipped or not
 
 if( items.ContainsKey("item1"))// check dictionary has "item1"
 {
     bool isEquipped = items["item1"];
     if(isEquipped)Debug.Log("item1 is equipped");
     else Debug.Log("item1 is not equipped");
 
 }
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

Count object list duplicates 1 Answer

Inventory System wont work 2 Answers

C# Lists & Inventory Logic 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