Search Unity

Changing Material Color using Sliders

Discussion in 'Scripting' started by NDSno1, Oct 27, 2016.

  1. NDSno1

    NDSno1

    Joined:
    Dec 20, 2014
    Posts:
    223
    Hi all,

    I'm trying to change color of materials by using UI sliders. My idea is that each gameObject will have a CarInfo.cs class that holds the list of materials to be colored. After that, the ColorChanger.cs script will access this CarInfo.cs on the gameobject, get the list of materials and apply color to those material. However, I still can't get it to work. These are the code for 2 scripts.

    ColorChanger.cs
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class ColorChanger : MonoBehaviour {
    6.     public Slider red;
    7.     public Slider green;
    8.     public Slider blue;
    9.  
    10.     public Color newColor;
    11.     public Material[] materialToColor;
    12.     public CarInfo carToColor;
    13.  
    14.     // Use this for initialization
    15.     void Start () {
    16.         //red.GetComponent<Slider>();
    17.         //green.GetComponent<Slider>();
    18.         //blue.GetComponent<Slider>();
    19.         materialToColor = carToColor.colorMaterial;
    20.     }
    21.  
    22.     public void ApplyColor()
    23.     {
    24.         for (int i = 0; i < materialToColor.Length - 1; i++)
    25.         {
    26.             materialToColor[i].color = newColor;
    27.         }
    28.         //carToColor.carColor = newColor;
    29.     }
    30.  
    31.     // Update is called once per frame
    32.     void Update () {
    33.         newColor = new Color(red.value, green.value, blue.value, 1f);
    34.         ApplyColor();
    35.     }
    36. }
    37.  
    CarInfo.cs
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using System.Collections.Generic;
    5.  
    6. /*
    7. * This class is to be attached to all cars
    8. * contains car information for simulation
    9. * information are finalized values:
    10. * taken from base value, user upgrade and user tuning
    11. * Also used for changing car color
    12. * by applying color values to array of materials
    13. */
    14. public class CarInfo : MonoBehaviour {
    15.  
    16.     public Color carColor = new Color(1f,1f,1f);
    17.     public float red = 1f;
    18.     public float green = 1f;
    19.     public float blue = 1f;
    20.     //list of material that is going to be change color
    21.     //accessed by color changer
    22.     public Material[] colorMaterial;
    23.     // Use this for initialization
    24.     void Start () {
    25.  
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void Update()
    30.     {
    31.         //changing car body color by changing color of all materials applied to parts of body
    32.         for (int i = 0; i < colorMaterial.Length - 1; i++)
    33.         {
    34.             colorMaterial[i].color = carColor;
    35.         }
    36.     }
    37. }
    38.  
    Oh also I attached ColorChanger.cs to a panel in a canvas
     
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Becuase every Update ColorChanger is setting the color to the slider values, but then every Update CarInfo is putting it back to white:
    Code (CSharp):
    1. void Update()
    2.     {
    3.         //changing car body color by changing color of all materials applied to parts of body
    4.         for (int i = 0; i < colorMaterial.Length - 1; i++)
    5.         {
    6.             colorMaterial[i].color = carColor;
    7.         }
    8.     }
    This code should be in start or Awake if you only want it to run one time which you do, I'm sure. otherwise every Frame your resetting all the materials colors back to white. Also i think you want this check to be :
    Code (CSharp):
    1. for (int i = 0; i < colorMaterial.Length ; i++)
    Your code skips the last material in the array
     
  3. NDSno1

    NDSno1

    Joined:
    Dec 20, 2014
    Posts:
    223
    Thanks, got it to work. The problems are the exact 2 as you said:
    The update() in carinfo keeps changing the color back
    i forgot the last material
    This is the final code as a thank you:
    CarInfo.cs
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using System.Collections.Generic;
    5.  
    6. /*
    7. * This class is to be attached to all cars
    8. * contains car information for simulation
    9. * information are finalized values:
    10. * taken from base value, user upgrade and user tuning
    11. * Also used for changing car color
    12. * by applying color values to array of materials
    13. */
    14. public class CarInfo : MonoBehaviour {
    15.  
    16.     public Color carColor;
    17.     public float red = 1f;
    18.     public float green = 1f;
    19.     public float blue = 1f;
    20.     //list of material that is going to be change color
    21.     //accessed by color changer
    22.     public Material[] colorMaterial;
    23.     // Use this for initialization
    24.     void Start () {
    25.         //changing car body color by changing color of all materials applied to parts of body
    26.         //dont need this. ColorChanger already feeding color to material
    27.         //This class only need to contain the list of color
    28.         //for (int i = 0; i < colorMaterial.Length; i++)
    29.         //{
    30.         //    colorMaterial[i].color = carColor;
    31.         //}
    32.     }
    33.  
    34.     // Update is called once per frame
    35.     void Update()
    36.     {
    37.        
    38.     }
    39. }
    ColorChanger.cs
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class ColorChanger : MonoBehaviour {
    6.     public Slider red;
    7.     public Slider green;
    8.     public Slider blue;
    9.  
    10.     public Color newColor;
    11.     public Material[] materialToColor;
    12.     public CarInfo carToColor;
    13.  
    14.     // Use this for initialization
    15.     void Start () {
    16.         //red.GetComponent<Slider>();
    17.         //green.GetComponent<Slider>();
    18.         //blue.GetComponent<Slider>();
    19.         materialToColor = carToColor.colorMaterial;
    20.     }
    21.    
    22.     public void ApplyColor()
    23.     {
    24.         for (int i = 0; i < materialToColor.Length; i++)
    25.         {
    26.             materialToColor[i].color = newColor;
    27.         }
    28.         //carToColor.carColor = newColor;
    29.     }
    30.  
    31.     // Update is called once per frame
    32.     void Update () {
    33.         newColor = new Color(red.value, green.value, blue.value);
    34.         ApplyColor();
    35.     }
    36. }
    37.  
     
  4. btyson_z

    btyson_z

    Joined:
    Aug 10, 2018
    Posts:
    9
    Any chance you could make all this public for an artist to use and repost? I want to set values on a material/shader I already have.
     
  5. NDSno1

    NDSno1

    Joined:
    Dec 20, 2014
    Posts:
    223
    Sure :). Just take the code above, that's the complete code that I used :)