- Home /
Color Sensing or Changing not working
I am an amateur programmer with unity. But I am trying to change the rgba color of an object in a sequence that will sequence through every color (discluding shades of a color) (in a fashionable rgba order) but the color will go through the stages of only one color changing and staying at the endpoint. Here is my code: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Rainbow_Material : MonoBehaviour {
public float speed;
bool greenUp, blueUp, redUp, greenDown, blueDown, redDown;
public Material colour;
float green, red, blue;
// Use this for initialization
void Start () {
//colour = gameObject.GetComponent<Material> ();
greenDown = false;
blueUp = false;
blueDown = false;
redUp = false;
redDown = false;
greenUp = false;
}
// Update is called once per frame
void Update () {
Debug.Log (red + " " + green + " " + blue);
green = colour.color.g;
blue = colour.color.b;
red = colour.color.r;
//Trigger to change the changing color
if (red <= 1 && green <= 0 && blue == 0) {
greenDown = false;
blueUp = false;
blueDown = false;
redUp = false;
redDown = false;
greenUp = true;
}
if (red <= 1 && green <= 1 && blue == 0) {
greenDown = false;
blueUp = true;
blueDown = false;
redUp = false;
redDown = false;
greenUp = false;
}
if (red <= 1 && green <= 1 && blue <= 1) {
greenDown = false;
blueUp = false;
blueDown = false;
redUp = false;
redDown = true;
greenUp = false;
}
if (red == 0 && green <= 1 && blue <= 1) {
greenDown = true;
blueUp = false;
blueDown = false;
redUp = false;
redDown = false;
greenUp = false;
}
if (red == 0 && green == 0 && blue <= 1) {
greenDown = false;
blueUp = false;
blueDown = true;
redUp = false;
redDown = false;
greenUp = false;
}
if (red == 0 && green == 0 && blue == 0) {
greenDown = false;
blueUp = false;
blueDown = false;
redUp = true;
redDown = false;
greenUp = false;
}
// Changes the color
if (greenUp) {
colour.color = new Color(1, green + speed, 0, 0);
}
if (greenDown) {
colour.color = new Color(0, green - speed, 1, 0);
}
if (redUp) {
colour.color = new Color(red + speed, 0, 0, 0);
}
if (redDown) {
colour.color = new Color(red - speed, 1, 1, 0);
}
if (blueUp) {
colour.color = new Color(1, 1, blue + speed, 0);
}
if (blueDown) {
colour.color = new Color(0, 0, blue - speed, 0);
}
}
}
If anyone can assist me with the problem in my code, or/and tell me whether or not its a problem with my code to change the color, or with the code to sense the color. That would be very helpful.
Answer by KittenSnipes · Apr 15, 2018 at 02:05 PM
Hope this helps. Cheers!
//List of colors to change to
public List<Color> colors;
//Time between each color change
public float timeTilColorChange = 0.05;
//Used to reference our renderer
Renderer rend;
//Reference of the current color our material is
int currentColor;
//Reference of the time
float timer;
void Start() {
//Set our defaults up
currentColor = 0;
timer = 0;
rend = GetComponent<Renderer>();
if(color.Count <= 0) {
colors.Add(Color.black);
colors.Add(Color.blue);
colors.Add(Color.cyan);
colors.Add(Color.gray);
colors.Add(Color.green);
colors.Add(Color.magenta);
colors.Add(Color.red);
colors.Add(Color.yellow);
}
}
void Update() {
//Add to our timer in real time
timer += Time.deltaTime;
// If our timer is greater than or equal to our timeTilColorChange
if (timer >= timeTilColorChange) {
//And if our currentColor is not at the end of our list
if (currentColor < colors.Count) {
//Access our renderers material color and change it to our current color
rend.material.color = colors[currentColor];
//Move on to the next color in our list
currentColor++;
}
//If we are at the end of the list
else {
//Start at the beginning of our list
currentColor = 0;
//Access our renderers material color and change it to our current color
rend.material.color = colors[currentColor];
//Move on to the next color in our list
currentColor++;
}
//Restart our timer
timer = 0;
}
}
Thanks, but I already found an even simpler way of doing it. It involves the line of code:
Renderer.material.color.HSVToRGB(value + speed, 1, 1);
But thanks anyways.