Select random object in List using Linq

Hey all,

I’m in the process of creating an objectRecycler with the base code provided by prime31 in one of his youtube tutorials. Linq is completely new to me, but I have some SQL knowledge so it’s not completely new territory I guess.

What I need to do is select a random GO in my List that also is also set to active == false. I’ve looked through the syntax from googling my question, but I can’t quite wrap my head around it. Here’s my query:

			GameObject freeObject = ( from item in objectList
						              where == false
						              select item ).FirstOrDefault;

Is there any short and simple way to select a random object that is qualified under my query as well? I’d really like to stick with Linq as it has some great functionality and I’d like to start incorporating it in lots of places, so please refrain from posting alternative methods ( ie standard array shuffling ). I also know that utilizing the extension method FirstOrDefault may not be appropriate for my selection, and I’m reading through the MSDN documentation but as I’m sure most of you know, Microsoft’s documentation is definitely not novice friendly. Thanks in advance.

I semi-figured it out:

var randomSelection = objectList.Where( i => == false );
var freeObject = randomSelection.ElementAtOrDefault( new System.Random().Next() % randomSelection.Count() );

The reason I say ‘semi’ is because now I have to set my objectRecycler class to inherit from MonoBehaviour, and on top of that I have to add ‘using System;’ as an assembly reference, which might be adding unneeded overhead to my project size. Not only that, but when I reference ‘Random.Range’, I now have to reference which assembly it is coming from such as ‘UnityEngine.Random.Range’ and ‘System.Object.Instantiate’ and such.

If anybody has a better solution, I’m all ears. This solution will do for now, but if there is something else that I can do that doesn’t add assembly overhead I would very much appreciate the suggestions.