- Home /
Is it not possible to include "if-then" conditionals within foreach loops?
Hello.
If I throw a conditional in a foreach loop - the following does nothing:
Transform[] transformComponents = GetComponentsInChildren<Transform>();
foreach (Transform component in transformComponents)
{
if (component.transform.name=="CarteBallY")
{
component.localScale = new Vector3(20,20,20);
}
}
If I get rid of the conditional "if-then" and just adjust the scale per object in the array, it works fine. What am I not understanding?
Just before your if statement add:
Debug.Log("Name = "+component.transform.name); Is the name output to the console identical (including case and any spaces") to "CarteBallY"?
I cannot reproduce your bug. Please follow @robertbu's advice and report back!
It seems that I can't reproduce it either - a Unit Test proves that this should and does work. $$anonymous$$ust be... an issue elsewhere. Please forgive me for wasting your time.
Answer by Wolfram · Feb 03, 2013 at 04:02 PM
The only possible explanation is a spelling problem (a space behind your actual object name ("CarteBallY "), or an I (upper case i) instead of an l (lower case L) ("CarteBaIIY"), or wrong case ("CarteBally"), as suggested by @robertbu. Also make sure the script is attached to a GameObject that actually has a child object named "CarteBallY".