Material.SetColor Paradox

Hi all,
So I’m on the merry journey of creating my first Unity game, but hit another small hiccup. So, let me explain:
I know I can use:

gameobjectname.renderer.material.SetColor("_Color", colorVar);

I know that because I read the script reference, and made a simple test scene with a primitive to test it. It works perfectly.

But for some reason, in my game, it doesn’t work, visually the colour remains unchanged. But, if I do GetColor for the material I just changed, before the change code, and after it, the two values are different - as if the colour has been changed. Also, I get no errors whilst the scene is running.

This code is part of quite a long function that is basically loading a save game, but here’s the relevant section:

GameObject stockShell = GameObject.Find("stockShell");
				// convert hex to colour
				Color playerCarColourA = HexToColor(currentCarSplitPaintHexA);
				Color playerCarColourB = HexToColor(currentCarSplitPaintHexB);
			// apply player colour			
				stockShell.renderer.material.SetColor("_Color", playerCarColourA);
				stockShell.renderer.material.SetColor("_Color2", playerCarColourB);

I’ve tried changing playerCarColourA and B to things like Color.red - and I get the same result: no visual change, but doing a before and after GetColor, the console is telling me the value has changed. No errors. But no visual change.

Any ideas folks?
Thanks
Matt

If the value is right but you don’t see a difference… do you use the right name for your shader?
Sometimes there are several colors and textures, maybe you got the wrong one. Which shader are you using?

It’s a car paint shader. By shader name do you mean the _Color and _Color2 values? I’ve checked inside the shader and they are the correct names for the properties.

I was wondering, if maybe there was a way to reference the name of the shader- carBody, and change it directly, rather having to go stockShell.renderer.material etc.

Do you have a test scene?

It sounds like there is a problem with the shader, or the way the shader was designed, especially if you are getting the value back after a GetColor. When you tested in the primitive, was it with the car shader?

Yes, the test scene is using the same shader and it works fine.

Out of curiosity I added one of my car prefabs to the test scene, and tried to change the colour on it using the same technique- and it works fine. Now, I’m really confused :face_with_spiral_eyes:

Edit: Turns out I was just being a noob. Although the GameObject I was referencing wasn’t giving a error, I just created a new one and assigned it to the right part and it works now.

D’oh.