Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Todd0824 · Jul 12, 2015 at 09:40 AM · unity 5listdictionary

JS Dictionary?

Hi All, I've encountered a little problem lately. So I have a whole bunch of gameobjects and they all have the same script attached to them and there is the same int in the script but the int is different the gameobjects.

So I was trying to find a way to store all the gameobjects and their own different int in a dictionary, so for example, if I pick a int, I would find the gameobject that it is appointed to the int.

Anyway of doing this? I would really like to know. Thankyou. (I am writing in javascript)

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
1
Best Answer

Answer by fafase · Jul 12, 2015 at 11:44 AM

Dictionary is one way to go if the int values are not all consecutive. In this case, considering it goes from 0 to n and never skip a value in between, you can just use an array.

You could also use an ordered list, that is if you consider iterating through the collection based on the int value.

But if you are about to get any value, even negative, and you need to find based on the int value, then dictionary is the one.

 var dict = new Dictionary.<int, GameObject>();
 var objs = FindObjectsOfType(ScriptName);
 foreach(var item in objs){
     dict.Add(item.intValue,item.gameObject);
 }


and use as :

 if(dict.Contains(10)){
    var obj : GameObject = dict[10];
 }
Comment
Add comment · Show 4 · 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 Todd0824 · Jul 12, 2015 at 04:34 PM 0
Share

I'm getting a new error:

 ArgumentException: An element with the same key already exists in the dictionary

Here is my code:

 import System.Collections.Generic;
 
 private var openList = new Dictionary.<int, GameObject>();
 private var Nodes : GameObject[];

 function Awake()
 {
     Nodes = GameObject.FindGameObjectsWithTag("Node");
 }

 function FixedUpdate()
 {
     PickAnd$$anonymous$$ove();
 }
 
 function PickAnd$$anonymous$$ove()
 {
     for(Node in Nodes)
     {
         var nodeInfo : NodeInfo = Node.GetComponent(NodeInfo);
         
         if(nodeInfo.Opened == true) 
         {
             openList.Add(nodeInfo.F, Node);
         }
     }
 }

What am I doing wrong?

avatar image fafase · Jul 12, 2015 at 08:07 PM 0
Share

You cannot have twice the same key in a dictionary. The point of it is that if you provide the key, you get the value, but if there is twice the same key, the compiler cannot know which you meant. So this is forbidden.

You said you want to provide the int value and get the matching object, that is not possible if you have many times the same values.

Also what you are doing there is not appropriate. Fetching component in an update, particularly for a collection is not so good. You should ins$$anonymous$$d have a collection of the script already, found once in Start. If you were to create new objects later on, add them to the existing list.

For your original issue, it is important to know if the F value is consecutive, that is it goes 0,1,2,3,... so that you can use a array of array for instance with the value a index of the main array.

avatar image Todd0824 · Jul 12, 2015 at 08:26 PM 0
Share

So the F value can't be the same? And I don't quite understand what do you mean by "array of array of instance with the value a index of the main array." Anyway, thankyou for your time!

avatar image fafase · Jul 13, 2015 at 06:18 PM 0
Share

If you want to use dictionary, F values have to all be unique. This is the issue you experienced. Dictionaries have to have unique keys.

If you need to have duplicate of F values, then a solution would be an array of array. Search for jagged array.

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

2 People are following this question.

avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Changing Custom Object Variable 1 Answer

Combine Children Dictionary in place of Hashtable? 2 Answers

Nested Dictionary Change Value Problem 3 Answers

Two dynamic dropdowns from one source list 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