using UnityEngine;
using System.Collections;
public class hfsgthd : MonoBehaviour {
public Color color = Color.black;
void Example() {
color.g = 0f;
color.r = 0f;
color.b = 0f;
color.a = 0f;
}
void Start ()
{
gameObject.GetComponent<Renderer>().material.color = new Color (color.r,color.g,color.b,color.a);
}
void Update() {
if (Input.GetKeyDown (KeyCode.G))
color.g = color.g + (float)0.1f;
if (Input.GetKeyDown (KeyCode.R))
color.r = color.a + (float)0.1f;
if (Input.GetKeyDown (KeyCode.B))
color.b = color.b + (float)0.1f;
if (Input.GetKeyDown (KeyCode.A))
color.a = color.a + (float)0.1f;
}
}
It works but doesn’t modify the material.
Can you help me?
you are never reasigning the material.color to the new color. You are changing the color values, but not applying to the material.
void Update(){
if (Input.GetKeyDown (KeyCode.R)){
color.r = color.a + (float)0.1f;
//Assign the changed color to the material.
gameObject.Renderer.Material.Color = color;
}
}
Full Script:
using UnityEngine;
using System.Collections;
public class hfsgthd : MonoBehaviour {
public Color altColor = Color.black;
public Renderer rend;
//I do not know why you need this?
void Example() {
altColor.g = 0f;
altColor.r = 0f;
altColor.b = 0f;
altColor.a = 0f;
}
void Start ()
{
//Call Example to set all color values to zero.
Example();
//Get the renderer of the object so we can access the color
rend = GetComponent<Renderer>();
//Set the initial color (0f,0f,0f,0f)
rend.material.color = altColor;
}
void Update()
{
if (Input.GetKeyDown (KeyCode.G)){
//Alter the color
altColor.g += 0.1f;
//Assign the changed color to the material.
rend.material.color = altColor;
}
if (Input.GetKeyDown (KeyCode.R)){
//Alter the color
altColor.r += 0.1f;
//Assign the changed color to the material.
rend.material.color = altColor;
}
if (Input.GetKeyDown (KeyCode.B)){
//Alter the color
altColor.b += 0.1f;
//Assign the changed color to the material.
rend.material.color = altColor;
}
if (Input.GetKeyDown (KeyCode.A)){
//Alter the color
altColor.a += 0.1f;
//Assign the changed color to the material.
rend.material.color = altColor;
}
}
}