Search Unity

Unity Event Color Selector Trouble

Discussion in 'Scripting' started by ImDahm, Aug 22, 2017.

  1. ImDahm

    ImDahm

    Joined:
    May 24, 2017
    Posts:
    2
    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)
    Code (CSharp):
    1.    
    2.     //EVENT
    3.     public UnityEvent e_OnColorChanged;
    4.  
    5.     //RGB Sliders
    6.     public Slider sliderR;
    7.     public Slider sliderG;
    8.     public Slider sliderB;
    9.  
    10.     void Start(){
    11.         sliderR.onValueChanged.AddListener(updateColor);
    12.         sliderG.onValueChanged.AddListener(updateColor);
    13.         sliderB.onValueChanged.AddListener(updateColor);
    14.    }
    15.  
    16.     private void updateColor(float arg) {
    17.         color = new Color(sliderR == null ? 255 : sliderR.normalizedValue,
    18.                           sliderG == null ? 255 : sliderG.normalizedValue,
    19.                           sliderB == null ? 255 : sliderB.normalizedValue);
    20.  
    21.         if (OnColorChanged != null) {
    22.             OnColorChanged();
    23.         }
    24.     }
    25.  
    Character Creation Menu (Shortened)
    Code (CSharp):
    1.  
    2.     void Start(){
    3.         GameObject.Find("Button_Skin_Color").GetComponent<Button>().onClick.AddListener(onclicktest);
    4.     }
    5.  
    6.     private void onclicktest() {
    7.         colorSelector.OnColorChanged.AddListener(ApplyColorToObject);
    8.     }
    9.  
    10.     private void ApplyColorToObject(Color newColor) {
    11.         GameObject.Find("Button_Skin_Color").GetComponent<Image>().material.color = newColor;
    12.     }
    13.  
    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
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    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.
     
    ImDahm likes this.
  3. ImDahm

    ImDahm

    Joined:
    May 24, 2017
    Posts:
    2
    Exactly!!! Thanks!!
     
    LiterallyJeff likes this.