I’m trying to loop through all game objects in a hierarchy list. If I do this, it works ok:
foreach(Transform t in transform.parent.transform)
{
Debug.Log(t.name);
}
I get this in the console:
- background
- arrow
- selection
This is fine, these three are the objects who have sprite renderers and I want to disable these renderers.
So then I do this:
foreach(Transform t in transform.parent.transform)
{
SpriteRenderer myRenderer = GetComponent<SpriteRenderer>();
myRenderer.enabled = false;
}
This doesn’t work as I intend it. It only disables the sprite renderer of one of the objects, the arrow.
How come? Can somebody help me out with this?
This might be of importance, the code that does this is on the arrow object. But since I do transform.parent.transform, and using Debug.Log on the names on each of those returns all three objects, I don’t understand why it doesn’t actually work.