How to use System.Linq in unityscript

Hello !
I wan’t to use System.Linq in unityscript.
I try this :

var currentSlot : Slot = slotsList.Where(p => p.currentItem == null).First() ;

but it doesn’t work : Unexpected token: p

Maybe wrong syntax ?

You can’t use Linq in unityscript.

Yes, you can. eg :

2 Likes

import System.Linq!

Didn’t know that. Nice one.

I try this :

var currentSlot : Slot = slotsList.Where(function(p) => p.currentItem == null).First() ;

but doesn’t work again ? What’s the correct syntax ?

Make sure to declare you are using LINQ in your script :

#pragma strict
// declare LINQ namespace here
import System.Linq;

function DoStuff()
{
   var currentSlot : Slot = slotsList.Where(p => p.currentItem == null).First() ;
}
var ItemList : List.<GameObject>;
...
item = ItemList.Where(function(obj) { obj.name == "name of object"; }).First();

Note the syntax of ‘function’, with curly braces. Also note semi-colon ‘;’ before the terminating curly brace.