How to fade material on many children

I’m sure this should be straight forward, but I’m new to C# and unity.

I can fade out a single object no problem using:

myRenderer = GetComponent();   


myRenderer.material.color = Color.Lerp(fromColor, toColor, t / fadeTime);   

(full code below.)

But I can’t do this for grouped objects. I have a lot of objects, also with nested groups and I can’t figure out how to best cycle through to fade out all the items.

I would have thought I’d need to get myRedender into an array, but GetComponent doesn’t like that.

I have tried Fade out children of gameobject - Questions & Answers - Unity Discussions
But this only seems to change the colour not the alpha.

Any help on this would be greatly appreciated.

Thanks in advance for any guidance.

public class Muscles : MonoBehaviour {
    public float fadeTime = 1f;
    Color solidColor;
    Color fadedColor;
    Color currentColor;
    bool fading;
    bool faded;
    Renderer myRenderer;

    // Use this for initialization
    void Start()
        myRenderer = GetComponent<Renderer>();
        solidColor = myRenderer.material.color;
        fadedColor = new Color(solidColor.r, solidColor.g, solidColor.b, 0f);

    public void startFading()
       // go = true;
        if (!fading)


    IEnumerator Fade()
        Debug.Log("coruntine started for muscles");
        fading = true;
        Color fromColor = faded ? fadedColor : solidColor;
        Color toColor = faded ? solidColor : fadedColor;
        for (var t = 0f; t < fadeTime; t += Time.deltaTime)
            Debug.Log("fading muscles");
            //currentColor = Color.Lerp(fromColor, toColor, t / fadeTime);
            myRenderer.material.color = Color.Lerp(fromColor, toColor, t / fadeTime);
            yield return null;

        fading = false;
        faded = !faded;

Good day.

You only need to use a foreach loop, for example, set the TAG of all the objects you need to edit to “Cube”, then use something like this:

foreach (GameObject OneCube in GameObject.FindObjectsWithTag("Cube"))
 // Edit what you need to OneCube object, so each Objects in the scene tagged as "Cube" will be   edited in every foreach iteration


Ok, it’s me being thick!
I wasn’t going through the foreach loop correctly. I wasn’t referring back to the right object.
I changed it to:

            foreach (GameObject muscle in GameObject.FindGameObjectsWithTag("Muscle"))
                myRenderer = muscle.GetComponent<Renderer>();
                myRenderer.material.color = Color.Lerp(fromColor, toColor, t / fadeTime);

That’s sorted it. I forgot to refer back to “GameObject muscle” when assigning the MyRenderer.

Its’ now working!! Thanks @tormentoarmagedoom that’s really helped much appreciated!!
Now I’ve just got to find a way to tag hundreds of objects!

Thanks again!