Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
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.

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

deleting a face of a proceduraly created mesh. 1 Answer

instantiate objects in menu 1 Answer

How to make border lines of mesh visible? 1 Answer

Understanding Add component and classes. 1 Answer


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