• 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 DragonReeper · Nov 01, 2010 at 08:10 AM · errorfor-loopforeach

newbie at C# needs help with for loop

Okay I started today with C# in Unity and was wondering if someone can hep me with this for loop:

        foreach(MeshRenderer mr in gameObject.GetComponentInChildren<MeshRenderer>()){
            mr.enabled=real;
        }
        foreach(MeshCollider col in gameObject.GetComponentInChildren<MeshRenderer>()){
            col.isTrigger= !real;
        }

All the error codes monodevelope gives me instantlly make my brain freeze over.

Even if you can just give me a goor refrence to work with.

Comment
Add comment · Show 2
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 DragonReeper · Nov 01, 2010 at 08:40 AM 0
Share

Oops I just saw the stupid mistake , it happend when I edited it to try and make it work

avatar image Proclyon · Nov 01, 2010 at 11:42 AM 0
Share

This is a for each loop , and is a bit of a special C# for loop which walks through all items in a specified container

do not confuse that for a common for loop to avoid errors in understanding questions answers and code.

2 Replies

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

Answer by Mike 3 · Nov 01, 2010 at 08:21 AM

The bottom one needs to use

gameObject.GetComponentInChildren<MeshCollider>()

instead of the same, but with MeshRenderer

The rest looks ok, though you don't need to use gameObject each time - GetComponentInChildren by itself will work fine

Comment
Add comment · Show 3 · 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 DragonReeper · Nov 01, 2010 at 08:38 AM 0
Share

Thanks alot for the help the <> syntax is a bit forieng to me but now I can get back to my script thanks again

avatar image Mike 3 · Nov 01, 2010 at 09:17 AM 0
Share

You'll get there, but if you want to research it at any point, google c# generics

avatar image DragonReeper · Nov 01, 2010 at 09:08 PM 0
Share

Reading about c# now, if you have anything else that can help me avoid noob mistakes like these I'd like to learn about them too

avatar image
1

Answer by DragonReeper · Nov 01, 2010 at 08:50 AM

Also to get a IEnumerat use GetComponents not GetComponent :P

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 Mike 3 · Nov 01, 2010 at 09:00 AM 0
Share

indeed, that'd help too :D

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

ScrollableList best way to implement 0 Answers

C# For loop in button to set gameObjects in array to active 1 Answer

Help solving for loop error 1 Answer

Get child position from last gameobject in array 2 Answers

Null Reference exception, in foreach loop in AI 2 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