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 menep · Jun 11, 2012 at 08:03 PM · c#classarrayliststruct

C# ArrayList access from other script

Ive got an error:

 Cannot cast from source type to destination type.

when I try to use foreach in other script:

 bInfo   =     gameCore.GetComponent<bInfo>();
 ArrayList boyList= bInfo.boyList;
 foreach (PlayerNode entry in boyList){}

The list contains of:

 public class PlayerNode
 {
     public string name;
     public string id;
 }
Comment
Add comment · Show 5
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 Eric5h5 · Jun 11, 2012 at 08:15 PM 1
Share

Why are you using ArrayList? Use a generic List.

avatar image menep · Jun 13, 2012 at 08:28 PM 0
Share

Yes generic List is good for one type list. When I have changed that to generic list I had some errors, because I used that list for other purposes. Now this list is generic one and it only holds Player info class.

But still , how can I cast from ArrayList if there are different objects inside?

avatar image whydoidoit · Jun 13, 2012 at 08:52 PM 0
Share

Generic lists can store anything if you use a base class - you should use them over an ArrayList.

I can't see your definitioni of boyList from the other script.

avatar image menep · Jun 13, 2012 at 09:53 PM 0
Share

Now it works as generic list. But still "how can I cast from ArrayList if there are different objects inside?"

avatar image whydoidoit · Jun 13, 2012 at 09:56 PM 0
Share

I can't see what it is defined as so it is very hard to help you.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by whydoidoit · Jun 13, 2012 at 09:58 PM

Generally you can extract elements from a collection of types using Linq.

Let's say you have a collection that contains many different things and you just want the list of the ones that are PlayerNodes:

   using System.Linq;
   ...
   foreach(var entry in bInfo.boyList.OfType<PlayerNode>())
   {
   }

I'm not 100% sure that this is your problem as I can't see the definition of boyList inside the bInfo script.

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Does every c# class need to have its own script with the class' name? 4 Answers

c# arraylist 1 Answer

Distribute terrain in zones 3 Answers

Accessing Enumeration from another script issue 1 Answer

c# - void method from external class. 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