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
0
Question by Hyperion · Dec 21, 2013 at 07:47 PM · c#errorlinq

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

Comment
Add comment · Show 3
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 Womrwood · Dec 21, 2013 at 08:07 PM 1
Share

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

avatar image Tomer-Barkan · Dec 22, 2013 at 05:24 AM 0
Share

Yep, you're misisng the using System.Linq; command, that will solve this error at least.

avatar image Hyperion · Dec 22, 2013 at 05:29 AM 0
Share

Yup, I added System.Linq and that did solve that error. Thank you.

1 Reply

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

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

Comment
Add comment · Show 11 · 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 vexe · Dec 21, 2013 at 08:48 PM 1
Share

@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.

avatar image Statement · Dec 21, 2013 at 09:24 PM 0
Share

@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.

avatar image Hyperion · Dec 21, 2013 at 09:28 PM 0
Share

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.

avatar image vexe · Dec 21, 2013 at 09:29 PM 0
Share

I think the same thing is about GetComponent< T >, its doc was always, a legend....

avatar image Statement · Dec 21, 2013 at 09:30 PM 0
Share

@Hyperion: If you're using Unity 4.2 and you don't have the generic overload, try the example that @vexe commented. It seems most correct in this case.

Show more comments

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

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

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


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