- Home /
Change material color with a three slider
Hi everyone,
I'm a beginner in programming and I'm trying to change the color of my material by using a UI with 3 sliders. I manage to change the color with the slider but it's only affect the script and not the material (I create a visualization of the color in the script, that I can see in the inspector of my mesh).
I'm very closed, I just need to match my color with the one of material and it will be good and I can't found how to do it.
Here the code:
public Color altColor;
public Renderer rend;
public Material colors;
void slider()
{
altColor.r = 0.5f;
altColor.g = 0.5f;
altColor.b = 1.0f;
}
void Start ()
{
slider();
Renderer rend = GetComponent<Renderer>();
rend.material.color = altColor;
//Material rend = GetComponent<Material>();
//rend.material.colorRed = altColor;
}
public void colorRed (float newRed)
{
altColor.r = newRed;
}
public void colorGreen(float newGreen)
{
altColor.g = newGreen;
}
public void colorBlue(float newBlue)
{
altColor.b = newBlue;
}
// Update is called once per frame
void Update () {
}
This modifies the altColor variable, but then you need to set the material colour again after the change :
public void colorRed (float newRed)
{
altColor.r = newRed;
rend.material.color = altColor;
}
Thanks for the reply alucardj and Graphics_Dev.
I knew I was close and the "rend.material.color = altColor;" work perfectly !! Thanks you so much.
And sorry for the late answer ;)
Answer by Graphics_Dev · Mar 23, 2016 at 03:51 PM
Thanks @alucardj for the corrective comment...here is my updated answer. (I removed the slider function and call because I'm assuming that's there only for testing?)
using UnityEngine;
using System.Collections;
public class ColorChanger : MonoBehaviour {
public Color altColor;
private Renderer rend;
void Start ()
{
// Make sure you only get the rend component here
// because when you get the color, you only store the
// values of the color, not access to the color of
// the material.
//
// Also, 'rend' accesses the declared, private renderer
// so this CAN'T be:
//
// Renderer rend = GetComponent<Renderer>();
//
// because that would store a new 'rend' that would not be
// accessible outside of this function.
rend = GetComponent<Renderer>();
}
// set r, g, and b floats with slider
public void colorRed (float newRed)
{
altColor.r = newRed;
// Here we get the color of the 'gotten' Renderer
rend.material.color = altColor;
}
public void colorGreen(float newGreen)
{
altColor.g = newGreen;
rend.material.color = altColor;
}
public void colorBlue(float newBlue)
{
altColor.b = newBlue;
rend.material.color = altColor;
}
}
The OP is calling the public functions from a UI slider. While setting the material colour in update will fix the problem, a more optimal solution would be to set the material colour after modifying the variable rather than in update.
Your answer
Follow this Question
Related Questions
Material doesn't have a color property '_Color' 4 Answers
Changing two different objects renderer colour 1 Answer
How to lerp smoothly between two numbers? 1 Answer
UI Triangle Color picker 0 Answers