- Home /
C# Finding Closest Instance LINQ Error
Hello Everyone,
I've got a player on an empty terrain that can create instances of one type of a turret. Each turret is supposed to find another turret closest to itself and then shoot a laser at it in order to create a laser fence. I'm really close to achieving this, except the final step keeps getting errors (shown at bottom).
My code for the turret:
using UnityEngine;
using System.Collections;
public class TurretFence : MonoBehaviour {
public LineRenderer laserPrefab;
private LineRenderer Laser;
private float laserEnergy= 0.01f;
private Animator thisAnimator;
private Transform thisLaserPoint;
void Start()
{
thisAnimator=GetComponent<Animator>();
thisLaserPoint= transform.FindChild("LaserPoint1");
InvokeRepeating("Scan",2,0.3f);
}
// Update is called once per frame
void Update ()
{
if (!thisAnimator.GetBool("NotUpright"))
{
Laser=Instantiate(laserPrefab) as LineRenderer;
if (Laser!=null)
{
Laser.SetPosition(0,thisLaserPoint.position);
Laser.SetPosition(1,nearestTurret.transform.position);
Destroy(Laser, laserEnergy);
}
}
}
void Scan ()
{
Transform nearestTurret = Tools.FindNearest<TurretFence>(transform.position);
}
}
My code for finding the closest instance by type:
using UnityEngine;
using System.Collections.Generic;
public class Tools {
public static T FindNearest<T>(Transform reference) where T : MonoBehaviour
{
return FindNearest<T>(reference.position);
}
public static T FindNearest<T>(Vector3 reference) where T : MonoBehaviour
{
List<T> list = GameObject.FindObjectsOfType<T>().ToList();
list.Sort(
(x, y) =>
(x.transform.position - reference).sqrMagnitude
.CompareTo((y.transform.position - reference).sqrMagnitude));
return list.First();
}
}
The two errors I'm getting are:
Tools.cs(13,43): error CS1501: No overload for method FindObjectsOfType' takes
0' arguments
and
Tools.cs(18,29): error CS1061: Type System.Collections.Generic.List' does not contain a definition for
First' and no extension method First' of type
System.Collections.Generic.List' could be found (are you missing a using directive or an assembly reference?)
I would really appreciate any help to solve these errors. There are no problems with the laser rendering, just the scanning process. And I have not misspelled anything.
Thank you,
-Hyperion
I guess what you would need to do is:
In your project options set "Runtime version" to "$$anonymous$$ono/.Net 3.5" Add reference to System.Core package (right click references in solution explorer) Add "using System.Linq" to your module after that your code should compile and execute
Yep, you're misisng the using System.Linq;
command, that will solve this error at least.
Yup, I added System.Linq and that did solve that error. Thank you.
Answer by Statement · Dec 21, 2013 at 08:07 PM
error CS1501: No overload for method FindObjectsOfType takes 0 arguments
What version of Unity are you using? FindObjectsOfType has a generic overload at least in Unity 4.3.2f1 and I don't understand why you'd get that error unless you were using a version that didn't support that overload.
error CS1061: Type System.Collections.Generic.List does not contain a definition for First and no extension method First of type System.Collections.Generic.List could be found (are you missing a using directive or an assembly reference?)
Check out the docs for First as it states which namespace it resides under.
(As Womrwood pointed out in their comment you forgot to include the System.Linq namespace)
[1]: http://docs.unity3d.com/Documentation/ScriptReference/Object.FindObjectsOfType.html
@Statement, actually there is a generic overload. But the docs never mentions it. Does anybody even read that doc? lol (no offense doc, but you suck) - Just type it out and the intellisense should spot it.
@Hyperion: Just make sure you put using System.Linq;
at the top of your file. For me, it didn't even let me do a ToList()
without it.
Even if there wasn't a generic version, you could use the normal one that takes a type - just pass it typeof(T)
:
List list = (GameObject.FindObjectsOfType(typeof(T)) as T[]).ToList();
As for First
, I suggest you use FirstOrDefault
from $$anonymous$$$ remarks:
The First(IEnumerable) method throws an exception if source contains no elements. To ins$$anonymous$$d return a default value when the source sequence is empty, use the FirstOrDefault method.
in other words, if your list was empty and you called First
on it, an exception will be thrown. But if you called FirstOrDefault
null will be returned so you could test the value from outside when you return.
@vexe; You're right. I can see there's an undocumented version of it. Not sure if this is a new addition or if it's age old. Since Hyperion is getting a compiler error stating there is no overload that accepts 0 arguments, I kind of get the impression that perhaps Hyperion is using an older version of Unity. Or I am totally blind to whatever else would cause that specific error in this case.
I believe I am using version 4.2. I shall first try doing what Womrwood said about making the compliler recognize the code, to see if it is just a compiler issue or not. Thank you all for your suggestions. I shall get back to you soon.
I think the same thing is about GetComponent< T >
, its doc was always, a legend....
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
how can i check if index exists? 2 Answers
DontDestroyOnLoad with object on another scene 2 Answers
Character moving forwards before pressing anything 0 Answers