- Home /
How to change the color of a material (material.color not working?)
Is there a way I can set the color of a material directly?
I feel like this should be a very simple thing, just materialA.color = this_color, but that doesn't seem to work. I've seen somewhere that I can set the material color to a predefined shader color or something (i haven't messed around with shaders yet), but I want the material color to change corresponding to the user's input.
Answer by OmegaFalcon · Jul 11, 2020 at 03:51 PM
Finally fixed my problem. It's actually very simple. Since I was using sprite lit, instead of using "_Color" I use "_BaseColor".
So doing material.setColor("_BaseColor", newcolor) is what worked.
Answer by haizathaneefa · Jun 29, 2020 at 04:35 AM
Hello, this might help you out with your problem here.
void Start()
{
var cubeRenderer = cube.GetComponent<Renderer>();
// change color to red
cubeRenderer.material.SetColor("_Color", Color.red);
}
looked promising but it didn't work idk why. The material just remains the color it was set to in the inspector.
Sorry, try this out. Place the script to the gameobject itself. Tested it myself and it works but just try it out on your own.
void Start()
{
gameObject.GetComponent<Renderer>().material.color = Color.red;
}
The thing is the material isn't a component of any object. I use the material in trail renderers, so to access the material I have a public reference to the material which I drag from the assets. When I do material.color = newcolor nothing changes.
I've tried changing the trail renderer's color property itself but got no results as well
Your answer

Follow this Question
Related Questions
2D Animation does not start 1 Answer
Shooting Towards mouse Cursor 1 Answer
Trying to reset lvl on collsion 1 Answer