Search Unity

How to use System.Linq in unityscript

Discussion in 'Scripting' started by Kooth, Mar 30, 2015.

  1. Kooth

    Kooth

    Joined:
    Feb 23, 2015
    Posts:
    53
    Hello !
    I wan't to use System.Linq in unityscript.
    I try this :
    Code (JavaScript):
    1. var currentSlot : Slot = slotsList.Where(p => p.currentItem == null).First() ;
    but it doesn't work : Unexpected token: p

    Maybe wrong syntax ?
     
  2. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    You can't use Linq in unityscript.
     
  3. AlucardJay

    AlucardJay

    Joined:
    May 28, 2012
    Posts:
    328
  4. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    import System.Linq!

    Didn't know that. Nice one.
     
  5. Kooth

    Kooth

    Joined:
    Feb 23, 2015
    Posts:
    53
  6. AlucardJay

    AlucardJay

    Joined:
    May 28, 2012
    Posts:
    328
    Make sure to declare you are using LINQ in your script :

    Code (csharp):
    1.  
    2. #pragma strict
    3. // declare LINQ namespace here
    4. import System.Linq;
    5.  
    6. function DoStuff()
    7. {
    8.    var currentSlot : Slot = slotsList.Where(p => p.currentItem == null).First() ;
    9. }
     
  7. faulknermano

    faulknermano

    Joined:
    Oct 30, 2015
    Posts:
    2
    Code (JavaScript):
    1.  
    2. var ItemList : List.<GameObject>;
    3. ...
    4. item = ItemList.Where(function(obj) { obj.name == "name of object"; }).First();
    5.  
    Note the syntax of 'function', with curly braces. Also note semi-colon ';' before the terminating curly brace.
     
    Last edited: Nov 4, 2015