- Home /
setColor function not working as intended
trying to make a function that highlights a certain object red, and then if it's clicked, the object is deleted. if the object is then not highlighted, every object placed removes the red tint. everything works up until the actual set color, including finding the game objects, I cant see what I'm doing wrong (ps. I'm still pretty inexperienced so any advice is appreciated I'm sure I'm doing smthn wrong)
public void removeMode()
{
//remove the selected object
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out aimHit))
{
if (aimHit.transform.gameObject.CompareTag("placeable"))
{
// color it to red
Material mat = aimHit.transform.GetComponent<Renderer>().material;
mat.SetColor("_Color", Color.red);
if (Input.GetKeyDown(KeyCode.Mouse0))
{
Destroy(aimHit.transform.gameObject);
}
}
}
//color it back to normal
if (!Physics.Raycast(ray, out aimHit) || !aimHit.transform.gameObject.CompareTag("placeable"))
{
for(int i = 0; i < GameObject.FindGameObjectsWithTag("placeable").Length; i++)
{
GameObject.FindGameObjectsWithTag("placeable")[i].transform.GetComponent<Renderer>().material.SetColor("Color", Color.clear);
}
}
}
Answer by tylersaur95 · Oct 26, 2021 at 11:02 PM
well, I found the problem, for whatever reason line 26 requires "_Color", not "Color". this makes it so the change actually takes effect. I guess ill leave this here in case someone experiences the same problem
Your answer
Follow this Question
Related Questions
Reflections disappear when changing materials color 2 Answers
After material.SetColor, SetTexture is ignored 1 Answer
Is it possiable to change the Emissive Color on VertexLit Blended Material with a script? 3 Answers
Setting color information and passing it in scenes 1 Answer
Changing the global fog (image effect) color from a different script, possible? 2 Answers