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 MountDoomTeam · Apr 22, 2013 at 01:41 AM · generic list

generic list Take not working?

it seems that generic list Take is not working in unity, can anyone confirm this?

It can be used to truncate

 var itemsOneThroughTwenty = myList.Take(20);
 var itemsFiveThroughTwenty = myList.Skip(5).Take(15);

if so, only certain elements of generic list are implemented? I can still do this :

myList.GetRange(0, 20);

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

2 Replies

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

Answer by Bunny83 · Apr 22, 2013 at 02:18 AM

This works in my Unity 4.0:

     // C#
     var L = new List<int>();
     for (int i = 0; i < 30; i++)
         L.Add(i);
     foreach(var val in L.Skip(2).Take(5))
         Debug.Log(val);

This prints :

 2
 3
 4
 5
 6

Are you sure you have a "using System.Linq;" in your script?
(and of course "using System.Collections.Generic;")
Skip / Take and a lot of other functions are Linq extention methods which work on generic IEnumerable. Those functions are not part of the List class, they can be used on any generic collection but only when you "import" them.

edit
Even when they don't exist they are not really hard to implement ;)

 public static IEnumerable<T> Take<T>(this IEnumerable<T> aSource, int aCount)
 {
     foreach(T val in aSource)
     {
        if (aCount >0)
            yield return val;
        else
            yield break;
        aCount--;
     }
 }

 public static IEnumerable<T> Skip<T>(this IEnumerable<T> aSource, int aCount)
 {
     foreach(T val in aSource)
     {
        if (aCount > 0)
        {
            aCount--;
            continue;
        }
        else
            yield return val;
     }
 }


Haven't testet it, but it should work ;) That's the whole magic.

Comment
Add comment · Show 1 · 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 whydoidoit · Apr 22, 2013 at 02:44 PM 0
Share

They exist in Unity - presu$$anonymous$$g Linq is imported. Perhaps not directly on List.

avatar image
2

Answer by hatshapedhat · Apr 22, 2013 at 01:47 AM

I believe Take came in with .NET 3.5 - and if I recall correctly Unity is only using .NET 2.

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

14 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 avatar image avatar image avatar image

Related Questions

Iterate through Generic List 2 Answers

how add values to Generic.list ? 2 Answers

Object transform.localScale not changing 0 Answers

How can I parent each item from one array to each item in another array? 1 Answer

Loading game objects into a generic list in specified order 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