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
1
Question by MinceD · Jul 17, 2010 at 02:43 PM · arraylistcs0117

ArrayList Problem. How to access using index.

Ok, so I'm having an issue with ArrayList. I seem to be unable to access the arraylist using an index. Look at the following example: (ps, Core.Score is a struct)

ArrayList HighScoreList = new ArrayList(); Core.Score tmpScore = new Core.Score(); tmpScore.Name = "ABRAKADABRA"; tmpScore.HighScore = 33234; tmpScore.yOffset = 0.3f; HighScoreList.Add(tmpScore);

So, if I now access this Highscorelist using a foreach loop, like so: foreach(Core.Score score in HighScoreList) { highscoreName.guiText.text = score.Name; } This works fine, I get ABRAKADABRA printed nicely.

However, using a simple index does not work at all.. I would have though i should be able to write: highscoreName.guiText.text = HighScoreList[0].Name;

But, it doesn't find the struct in index 0. I get this error: "Object" does not contain a definition for "Name".

So, what's going on here? Some hint would be fantastic :)

Thanks.

Kjetil

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

Answer by map-solar · Jul 17, 2010 at 02:59 PM

ArrayList contains all items as objects (that's where the error messages comes from - indeed, that Object does not contain a definition), so you have to cast them back to your struct.

highscoreName.guiText.text = ((Core.Score)HighScoreList[0]).Name;

The other option is to just use generics: List<T> from System.Collections.Generic

Comment
Add comment · Show 5 · 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 Mike 3 · Jul 17, 2010 at 03:17 PM 0
Share
  • for using List - it'll help with readability, and skips having to box and unbox the struct when getting or setting in the list

avatar image MinceD · Jul 17, 2010 at 03:23 PM 0
Share

Well, I tried something like that, and it gives me the same error. It's something about the struct. If I add a simple string in the highscore list, and access the index directly and using a string cast, then it works. But the example above does not. :/ I tried with a normal array, and that works fine, but I wanted to use the built in sorting method within ArrayList.

System.collections.generic does not seem to work for unity iphone.

avatar image MinceD · Jul 17, 2010 at 03:25 PM 0
Share

btw, related to the comment above, do I need to add some new references perhaps? currently I have the unity dll's + system, system.core and system.xml

avatar image Mike 3 · Jul 17, 2010 at 03:46 PM 1
Share

You need to change to a different compile profile (higher .NET version) to make it work. but I just noticed a problem with the code in the answer above. It should be ((Core.Score)HighScoreList[0]).Name; ins$$anonymous$$d

avatar image MinceD · Jul 17, 2010 at 04:05 PM 0
Share

yay,that worked! Thanks so much!

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

No one has followed this question yet.

Related Questions

ArrayList Transform type! 2 Answers

C# ArrayList access from other script 1 Answer

Problem with multi prefabs collision 1 Answer

how to change a variable from? an object in this array with a gui.button 0 Answers

Adding Waypoints using a List 2 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