- Home /
Sorting a list into multiple smaller lists
Im trying to get certain parts form a large list into a few smaller lists, and Im running into problems. I have it set up with a base Food Class. There is a Fruit and Veggie Class that extend from the Food Class and Apples and Orange Classes that extend from Fruit and Tomato and Kale Classes that extend from the Veggie class.
I threw all of my food into a list that was type Food. Im trying to use a for loop to find all the fruit veggies from that list and put all the fruit in a fruit list and all the veggies in a veggie list. Here is my script:
import System.Collections.Generic;
var apple : Apple;
var orange : Orange;
var tomato : Tomato;
var kale : Kale;
var cart : List.<Food>;
var fruitBag : List.<Fruit>;
var vegBag : List.<Veggie>;
function Start(){
cart = new List.<Food>();
fruitBag = new List.<Fruit>();
vegBag = new List.<Veggie>();
apple = new Apple();
orange = new Orange();
tomato = new Tomato();
kale = new Kale();
cart.Add(apple);
cart.Add(orange);
cart.Add(tomato);
cart.Add(kale);
for(var f : Fruit in cart){
fruitBag.Add(f);
}
for(var v : Veggie in cart){
vegBag.Add(v);
}
}
when I run the script I get an error that says:
InvalidCastException: Cannot cast from source type to destination type.
FoodCart.Start () (at Assets/Scripts/figuring shit out/FoodCart.js:30)
When I look in the inspector at runtime the fruit is added into the fruitBag, but the vegBag stays empty. Im guessing the script stops running because of the error, but the results from the component editor reads the fruit bag perfectly, even with the extra variable from the Fruit class that wasn't in the food class.
I tried doing it backwards and adding all the fruit into fruitBag and all the veggies into vegBag and then using a for loop on each list to add them all to the cart and it works, but I don't understand why it doesn't work the other way around. Im looking for a way to get the larger to sort into smaller lists because I was wanting to be able add to new lists from old lists both ways.
Any advice or nudges in the right direction would be greatly appreciated, thanks.
It looks like your trying to using List in javscript (.js), it is a C# class. (So import System.Collections.Generic doesn't work.)
@jogo13: No, List is not C#. It works fine in Unityscript. Note the slightly different syntax. (Also note how the code formatting is eating some of the tags. As a work around, type them with spaces, like var cart : List.< Food >
, then it doesn't get mangled.) There's not really any such thing as a "C# class" anyway; there are .NET classes, which apply to all languages in Unity.
Answer by Eric5h5 · Jan 28, 2013 at 07:39 AM
The issue is that "`for(var f : Fruit in cart)`" goes through all the items in the cart, not just the fruit. You'd need to check each item to see if it's a fruit type, then add it to the fruit list. Having classes for each fruit and veg isn't really right, though, if I understand what you're trying to do. You should have a class for fruit, and another for vegetables. As a basic example:
class Food {
}
class Fruit extends Food {
var name : String;
function Fruit (name : String) {
this.name = name;
}
}
class Veggie extends Food {
var name : String;
function Veggie (name : String) {
this.name = name;
}
}
function Start () {
var cart = new List.< Food >();
var fruitBag = new List.< Fruit >();
var vegBag = new List.< Veggie >();
cart.Add (new Fruit("apple"));
cart.Add (new Fruit("orange"));
cart.Add (new Fruit("tomato"));
cart.Add (new Veggie("kale"));
for (var item in cart) {
if (typeof(item) == Fruit) {
fruitBag.Add(item);
}
else if (typeof(item) == Veggie) {
vegBag.Add(item);
}
}
}
Ahhh, and the 2 fruits were the first thing in the cart list so it added those then threw an error when it got to the veggies which were not a fruit. Okay, I couldn't figure out for the life of me why if was adding the fruit and then stopping. Thanks for the clarification, it was very helpful.
I had one more question about the classes and stuff (I knew what I was doing didn't seem right) The classes I am making for my game (which are not fruit) have a lot of variables, and na$$anonymous$$g them all from the creation seems to be not the right way to go. Do people ever make classes of a different type with identical variables that are named? Or is that just crazy talk? Im new to this so this may sound weird, but how counter productive would it be to set it up like this.
I have food, fruit and veggie classes with like 12 variables. I make a separate class called like fruit stats that extends from a Stats class or something to give them all their variables.
It would be called like: apple = new Fruit(AppleStats());
and in the Fruit class itd look something like:
function Fruit( n : Stats){
name = n.name;
level = n.level;
etc etc etc on and on....
???
That would work. Some people prefer to pass in a single parameter that's a custom class rather than multiple parameters.
Your answer
![](https://koobas.hobune.stream/wayback/20220613091723im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Copy values between two classes in two lists. 1 Answer
Moving rigidbody from one list to another 2 Answers