- Home /
Question by
unity_fi43swFZZaqzPw · Aug 06, 2018 at 01:16 PM ·
scripting problemcasting
InvalidCastException
I get the invalid cast exception when excecuting the following line:
GameObject Target = (GameObject) SelOpt.Target[j];
Which seems odd as the items within the arraylist Target are gameobjects as seen in the constructor of the object below:
public Option(string Name, int TctCost, int OffCost, int Combat, GameObject Target) {
this.Target = new ArrayList();
this.Target.Add(Target);
this.TctCost = TctCost;
this.OffCost = OffCost;
this.Combat = Combat;
this.Name = Name;
}
So I'm trying to get a gameobject from an arraylist of gameobjects yet I get an invalid cast exception. Why is this?
Comment
Why do you use an ArrayList
ins$$anonymous$$d of a List<GameObject>
?
No particular reason actually. It fullfills the neads I have and I didn't think of using a List
Then, use a List<GameObject>
defined in the System.Collections.Generic
namespace.