- Home /
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;
}
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?
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.
Now it works as generic list. But still "how can I cast from ArrayList if there are different objects inside?"
I can't see what it is defined as so it is very hard to help you.
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.
Your answer
Follow this Question
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