• 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
Question by Fewpwew130 · Jul 20, 2019 at 10:32 AM · listvaluehashtableretrieve

[Simple] How to retrieve the Hashtable value, stored in a list?

Hello, guys! Could you please help me with retrieving the value of a key, contained in a list? Here is the script:

     public Hashtable all_containers_hashtable = new Hashtable();
 
     public List<string> container_list_1 = new List<string>();
     public List<string> container_list_2 = new List<string>();
 
 
     // Start is called before the first frame update
     void Start()
     {
         container_list_1.Add("name_1");
         container_list_1.Add("description_1");
         container_list_1.Add("param_1_1");
         container_list_1.Add("param_1_2");
         container_list_1.Add("param_1_3");
         all_containers_hashtable.Add("card_1", container_list_1);
 
         container_list_2.Add("name_2");
         container_list_2.Add("description_2");
         container_list_2.Add("param_2_1");
         container_list_2.Add("param_2_2");
         container_list_2.Add("param_2_3");
         all_containers_hashtable.Add("card_2", container_list_2);
 
 
     }
 
     // Update is called once per frame
     void Update()
     {
 
         if (Input.GetKeyDown(KeyCode.Q)) {
             Debug.Log(all_containers_hashtable["container_list_1"]);
 //returns the type: List
             Debug.Log(all_containers_hashtable["container_list_1"][1]); 
 // D o e s n ' t w o r k (intended, it will return "description_1"), instead it gives error
         }
 
 
     }
Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image
Best Answer

Answer by Dragate · Jul 20, 2019 at 03:24 PM

The hashtable doesn't have a "container_list_1" key. That is a value corresponding to the "card_1" key. Also, since hashtables can receive values of all types without pre-defining (in contrast to dictionaries), you need to typecast your value to access its elements.

 List<string> list = (List<string>) all_containers_hashtable ["card_1"]; // Get list by typecasting
 Debug.Log(list[1]); // Access the 2nd element of the list
Comment
Fewpwew130

People who like this

1 Show 3 · 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 Fewpwew130 · Jul 20, 2019 at 07:50 PM 0
Share

Thank you! It works! P.S. That solution requires the declaration (typecasting) of another variable - i.e. "list". Which increases the variable count in the script. Is there a way to get the element without that - another variable?

avatar image Dragate Fewpwew130 · Jul 20, 2019 at 08:05 PM 1
Share

It's just a local declaration. In one line it would be:

 Debug.Log(((List<string>) all_containers_hashtable ["card_1"])[1]);

It still would unbox the whole list temporarily to get hold of the element you want to access.

avatar image Fewpwew130 Dragate · Jul 20, 2019 at 08:19 PM 0
Share

Thank you! This is what I was looking for.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

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

changing reference types to value types 1 Answer

[common programming question] Custom array returns nothing 2 Answers

Get object to check list for number of specific items 0 Answers

Iterative list assigner with game objects 1 Answer

Add prefab to list based on object touched? 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