[Answered] Why this method does not work properly?

Hi.
I have just created a simple method which try to unify the all UI selectable elements’ color block at once.

    public void Unify(GameObject parent)
    {
        //initialize colorBlock
        ColorBlock colorBlock = new ColorBlock();
        colorBlock.normalColor = normalColor;
        colorBlock.highlightedColor = hilightedColor;
        colorBlock.pressedColor = pressedColor;
        colorBlock.selectedColor = selectedColor;
        colorBlock.disabledColor = disabledColor;

        int i = 0;

        List<GameObject> uiElements = GetAllChildren.GetAll(parent);
        foreach(GameObject go in uiElements)
        {
            if (go.GetComponent<Selectable>() != null)
            {
                go.GetComponent<Selectable>().colors = colorBlock;

                i++;
            }
        }

        Debug.Log($"<color=cyan>{i} UI selectable elements has been modified.</color>");
    }

When I call the function above, all ui selectable elements seems to be applied proper color in the editor property, but all of them displayed in grey and no selectedColor and/or pressedColor applied in the game view.


Instead doing above, by modifying the function as below, everything works just perfect but I do not understand what is the difference.

    public void Unify(GameObject parent)
    {

        int i = 0;

        List<GameObject> uiElements = GetAllChildren.GetAll(parent);
        foreach(GameObject go in uiElements)
        {
            if (go.GetComponent<Selectable>() != null)
            {
                var colors = go.GetComponent<Selectable>().colors;
                colors.normalColor = normalColor;
                colors.highlightedColor = hilightedColor;
                colors.pressedColor = pressedColor;
                colors.selectedColor = selectedColor;
                colors.disabledColor = disabledColor;
                go.GetComponent<Selectable>().colors = colors;

                i++;
            }
        }

        Debug.Log($"<color=cyan>{i} UI selectable elements has been modified.</color>");
    }

I should be misunderstand something. If anyone kindly could point out what it is, please do so.

In the second version of the script you are first copying the existing ColorBlock, then just modifying a few fields, and writing it back. This means that other fields besides the colors you are modifying are staying the same on the ColorBlock. This includes colorMultiplier, fadeDuration, etc… Struct ColorBlock | Unity UI | 1.0.0

In your first version you are creating a new ColorBlock. In a struct, the default new operator simply populates the struct with the “default” value for each type. That means 0 for floats, 0,0,0,0 colors, etc… (notice the 0s in the fields in your screenshot for Fade Duration etc) they might not be sensible values. But the copy you pull in the second version will have sensible values for these other fields.

I see!
I needed to look into the ColorBlock property in the link to study what it was. Now I understand why and it is not only about five colors for the element.
Thank you so much for the teaching!

p.s. BTW He/she looks cute just as how my cat does!