- Home /
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.
Oops I just saw the stupid mistake , it happend when I edited it to try and make it work
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.
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
Thanks alot for the help the <> syntax is a bit forieng to me but now I can get back to my script thanks again
You'll get there, but if you want to research it at any point, google c# generics
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
Answer by DragonReeper · Nov 01, 2010 at 08:50 AM
Also to get a IEnumerat use GetComponents not GetComponent :P
Your answer
Follow this Question
Related Questions
Error assigning to a variable 0 Answers
ScrollableList best way to implement 0 Answers
Error CS0029 Help? (Screenshot of Exact Error) 1 Answer
Not able to make a foreach loop 2 Answers
C# For loop in button to set gameObjects in array to active 1 Answer