Tinting objects

Hello everyone! When I create an object within a game I want it to change color to green. The scripting reference suggests using renderer.material.color, which doesn’t seem to work - the color doesn’t change. The compiler didn’t find any errors and the rest of the script works perfectly. Does any of you know what the problem might be? Or is there a better way to change object’s color?

Projection = (GameObject)Instantiate(BuildingPrefab, hit.point, Quaternion.identity);
Projection.renderer.material = new Material (Shader.Find(" Glossy"));
Projection.renderer.material.color = Color.green;

EDIT: Fixed it myself. The problem was that my prefab had more than one renderer attached to it.

where did you get the glossy shader from? (it's not a unity default) perhaps the shader doesnt have a color parameter to change

Ah, stupid me. That explains it - I found someone's post describing a similar problem and just copied the shader's name thinking it's default. I'll try to change it to something else now and say whether it helps. Thanks!

Same problem when using "Specular" or "Diffuse" shaders.

you want to use i think shader "material" then change color specular and diffuse shaders dont have color either I dont believe. (they affect how the object reacts to light but not the color of the object I THINK) the material if you have an object open in inspector and you mouse of the materials and you click on it and you select a material you select material and you have have a shader with a color option. if you select default diffuse for example you dont get that.

basically all the specular shader does is wash out an object at the point at whcih a light hits with super bright (normally white, though based on light color) light. it's that blinding white spot on metal hit by light. then diffuse is basically light hitting the object from a light source but lighting up areas other than the point of impact. the whole rest of the object being lit indirectly. That though doesnt affect the color. Color comes from the light, that just affects basically the brightness level per pixel of the object. Not the color of the pixels.

2 Answers

2

Hello again, I finally was able to change inistance’s color by changing prefab’s color before instantiating it:

BuildingPrefab2.renderer.material = new Material (Shader.Find("Diffuse"));
BuildingPrefab2.renderer.material.color = Color.green;
Projection = (GameObject)Instantiate(BuildingPrefab, hit.point, Quaternion.identity);

But I still can’t figure out how to change instance’s color AFTER instantiating it and without changing prefab’s color.

It should be Projection.renderer.color = Color.red; //etc

I'm not sure why you are creating a new material - doesn't the renderer already have one assigned?

Are you sure that it should be "Projection.renderer.color" and not "Projection.renderer.material.color"? I'm asking it because my script won't compile when I skip ".material". I wasn't changing the color of the material attached to the prefab because I thought it would change prefab's color as well. By the way, when I try this: Projection = (GameObject)Instantiate(BuildingPrefab,hit.point,Quaternion.identity); Projection.renderer.material.color = Color.green; the instance appears without any texture and is pink instead of green.

I'm asking why you are doing: new Material(Shader.Find("Diffuse")); Looks unnecessary to me.

Yes, that turned out to be unnecessary, thanks. By the way, I've already found the solution to this problem myself.

Fixed it myself. The problem was that my prefab had more than one renderer attached to it.