• 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
1
Question by FlamingTomato · Jan 11, 2015 at 06:02 PM · objectsfunctionstypeforin

for (var x in object) giving x = System.Collections.DictionaryEntry

So i have an object called listOfStuff, to which i add variables through

 function storeNew(item,value){
     listOfStuff[item] = value;
 }

then i have a loop function:

    function listData(){
             for(var x in listOfStuff){
                 Debug.Log(x + "is worth" + listOfStuff[x]);
                 }
     }

which should write all the data added to the object, so

 storeNew("banana",2);
 storeNew("diamond",1000);
 showData;
 //should result in: 
 //"banana is worth 2"
 //"diamond is worth 1000"

this only logs System.Collections.DictionaryEntry, why is that?

Comment
Add comment · Show 2
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 Scribe · Jan 11, 2015 at 06:09 PM 0
Share

so you call storeNew as:

 storeNew("banana",2);

aka

 listOfStuff["banana"] = 2;

"banana" tends not to be a standard index number...

and then say listOfStuff = new SomeClass[] then listOfStuff is an array of 'SomeClass' elements. Then your for loop is doing:

 for(var x in listOfStuff){
     // x is of type 'SomeClass' at this point
     // listOfStuff[x] is trying to do listOfStuff['SomeClass' instance] when it expects an integer index value
     Debug.Log(x + "is worth" + listOfStuff[x]);
 }
avatar image FlamingTomato · Jan 11, 2015 at 11:09 PM 0
Share

but listofstuff is not an array, but an object. It's created with var listOfstuff = {};

Also i tried entering a debug.log(listOfStuff[item]) into storeNew, and it properly logged the value as i wanted it to.

It seems to be just in the loop that it can't access it

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Scribe · Jan 12, 2015 at 09:07 AM

Okay so I don't really use unityscript any more and hadn't realized the meaning of the syntax but on a second look (and from the name of the error!) I'm guessing it is the unityscript equivalent of a C# dictionary so ~hopefully~ the following might work:

 var listOfstuff = {};
 
 function Start () {
     listOfstuff["test"] = 1;
     listOfstuff["test2"] = 2;
     
     for(var entry in listOfstuff){
         Debug.Log(String.Format("{0} is worth {1}", entry.Key, entry.Value));
     }
 }

so basically the important part is that to get teh first part you use entry.Key and the second part entry.Value!

Hope that helps, sorry for my rather sarky comment, I was definitely in the wrong :)

Scribe

Comment
Add comment · Show 2 · 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 FlamingTomato · Jan 12, 2015 at 05:14 PM 0
Share

Yes, that indeed works! Thanks a ton :D

avatar image Scribe · Jan 12, 2015 at 06:03 PM 0
Share

glad to hear it!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Drawing GUI onto multiple objects using for loops? 1 Answer

instantiate objects in menu 1 Answer

deleting a face of a proceduraly created mesh. 1 Answer

Hey! how to connect two objects? So to make one? 2 Answers

how to get objects that are in some radius with the point in certain center 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges