- Home /
Why isn't the color changing?
I made a very simple clock on Unity for practice, but for some reason the color of the hands won't change. I'm pretty sure I scripted it right and when I hit play the mesh render shows the correct color for the hands but that color isn't actually displayed. They seem to just stay a grey color. Here's my script and a screen shot of my Unity window. Thanks.
using UnityEngine;
using System;
public class ClockAnimator : MonoBehaviour {
private const float
hoursToDegrees = 360f / 12f,
minutesToDegrees = 360f / 60f,
secondsToDegrees = 360f / 60f;
public Transform hours, minutes, seconds;
public bool analog;
void Start () {
GameObject ClockBack = GameObject.FindGameObjectWithTag ("Back");
ClockBack.renderer.material.color = Color.black;
GameObject [] HandsArray = GameObject.FindGameObjectsWithTag ("Hand");
foreach (GameObject hand in HandsArray) {
hand.renderer.material.color = Color.yellow;
}
}
void Update () {
if (analog) {
TimeSpan timespan = DateTime.Now.TimeOfDay;
hours.localRotation = Quaternion.Euler (0f, 0f, (float) timespan.TotalHours * -hoursToDegrees);
minutes.localRotation = Quaternion.Euler (0f, 0f, (float) timespan.TotalMinutes * -minutesToDegrees);
seconds.localRotation = Quaternion.Euler (0f, 0f, (float) timespan.TotalSeconds * -secondsToDegrees);
}
else {
DateTime time = DateTime.Now;
hours.localRotation = Quaternion.Euler (0f, 0f, time.Hour * -hoursToDegrees);
minutes.localRotation = Quaternion.Euler (0f, 0f, time.Minute * -minutesToDegrees);
seconds.localRotation = Quaternion.Euler (0f, 0f, time.Second * -secondsToDegrees);
}
}
}
Can you confirm your tag is Hand (not hand, Hands, etc)?
How the color is handled totally is dependent on the shader. If the shader does not have a main color property, then the color won't change. Go to the material you are using for the hands. If you cannot change the color in the material, you won't be able to change it at runtime. Only a fraction of the standard shaders have a main color that can change.
Default Diffuse appears to be one that has a main color
Answer by SkaredCreations · Mar 22, 2014 at 07:28 PM
You are using a Diffuse shader, which requires a light to display correctly the colors, so just add a directional light rotated to be perpendicular to your clock.
Your answer
Follow this Question
Related Questions
Changing two different objects renderer colour 1 Answer
Material doesn't have a color property '_Color' 4 Answers
Get average color of the texture assigned to a mesh triangle 2 Answers
Material Instance Created in Car Model Prefab 0 Answers
Saving colors of multiple mesheseach having multiple materals 0 Answers