Unity Event Color Selector Trouble

I’m currently rewriting a character creation menu that I made a few weeks ago and I came across this weird with my color selector. I try using Unity events to notify the color selector observers that the color has changed, but even though only one button is supposed to be affected, the entire UI changes color and I don’t see any changes in the inspector’s materials.

Color Selector (Shortened)

    //EVENT
    public UnityEvent e_OnColorChanged;

    //RGB Sliders
    public Slider sliderR;
    public Slider sliderG;
    public Slider sliderB;

    void Start(){
        sliderR.onValueChanged.AddListener(updateColor);
        sliderG.onValueChanged.AddListener(updateColor);
        sliderB.onValueChanged.AddListener(updateColor);
   }

    private void updateColor(float arg) {
        color = new Color(sliderR == null ? 255 : sliderR.normalizedValue,
                          sliderG == null ? 255 : sliderG.normalizedValue,
                          sliderB == null ? 255 : sliderB.normalizedValue);

        if (OnColorChanged != null) {
            OnColorChanged();
        }
    }

Character Creation Menu (Shortened)

    void Start(){
        GameObject.Find("Button_Skin_Color").GetComponent<Button>().onClick.AddListener(onclicktest);
    }

    private void onclicktest() {
        colorSelector.OnColorChanged.AddListener(ApplyColorToObject);
    }

    private void ApplyColorToObject(Color newColor) {
        GameObject.Find("Button_Skin_Color").GetComponent<Image>().material.color = newColor;
    }

I tried with both delegate function and the above UnityEvent system, both giving me the same result. Anyone knows what I’m doing wrong? :l

I would guess it’s because all those Images are sharing the same default material, so altering one alters them all. Try instead setting the color field of the respective components, not the color field of the material.

1 Like

Exactly!!! Thanks!!

1 Like