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
0
Question by davidwalmsley · Jan 28, 2013 at 05:37 AM · listinheritancefor-loopsorting

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.

Comment
Add comment · Show 2
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 jogo13 · Jan 28, 2013 at 06:24 AM 0
Share

It looks like your trying to using List in javscript (.js), it is a C# class. (So import System.Collections.Generic doesn't work.)

avatar image Eric5h5 · Jan 28, 2013 at 07:13 AM 0
Share

@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.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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);
         }
     }
 }
Comment
Add comment · Show 2 · 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 davidwalmsley · Jan 28, 2013 at 01:41 PM 0
Share

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....

???

avatar image Eric5h5 · Jan 28, 2013 at 06:53 PM 0
Share

That would work. Some people prefer to pass in a single parameter that's a custom class rather than multiple parameters.

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

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Sorting a list by three parameters (C#) 1 Answer

How to sort a List by a class paramater 1 Answer


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