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 srsobieraj · May 07, 2012 at 06:08 AM · c#listgetcomponent

Accessing GetComponent from a C# List

Hi All,

I have a C# list of colliders (of type List<Collider>) and I am having a hard time accessing the GetComponent method from the colliders within the list.

Essentially, I assign a collider to the list after a successful raycast.

 Ray rayUnit = camera.ScreenPointToRay(Input.mousePosition);
 RaycastHit hitUnit = new RaycastHit();
 int unitLayer = LayerMask.NameToLayer("Unit");
 int unitMask = 1 << unitLayer;
 if(Physics.Raycast(rayUnit, out hitUnit, Mathf.Infinity, unitMask))
 {
     hitUnit.collider.GetComponent<Selectable>().selected = true;    
     selectedUnits.Add(hitUnit.collider);
 }

Then later I try to access the collider inside the List:

 for(int i=0; i < selectedUnits.Count; i++)
 {
     selectedUnits[i].GetComponent<Selectable>.selected = false;
 }

And this fails with the following error:

 error CS0119: Expression denotes a `method group', where a `variable', `value' or `type' was expected

Why can't I access the GetComponent method?

Thanks in advance for any help.

Comment
Add comment
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

1 Reply

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

Answer by rutter · May 07, 2012 at 06:31 AM

 GetComponent<Selectable>   <-- this is what you have, now
 GetComponent<Selectable>() <-- note the parens

A function name without parentheses is a reference to the function, which is useful for delegates and so on.

A function name with parentheses calls the function, which looks like what you meant to do here.

It's an easy mistake to make, right up there with missing semicolons and the like, but compilers are quite picky. :)

Comment
Add comment · Show 1 · 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 srsobieraj · May 07, 2012 at 11:50 PM 1
Share

Thanks. I was staring at this for quite some time yesterday. Good excuse to take a break I guess :)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Accessing GameObjects in my List 1 Answer

Get variable from Script in list and , set direct link to other script 1 Answer

Add and destroy instantiated gameobject in linkedlist C# 1 Answer

Multiple Cars not working 1 Answer


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