1
\$\begingroup\$

I wish to have the shader effect as in the game 'The stack' by ketchapp

https://play.google.com/store/apps/details?id=com.ketchapp.stack&hl=en

As you can see, the color of the objects as well as the skybox keep changing.

I'm completely c# noob and trying my best to find shaders to replicate the effect. But I can't find anything.

How can I change the color of an object at runtime?

\$\endgroup\$
  • \$\begingroup\$ We do not allow questions which ask for off-site resources, but we do answer how to solve specific problems. I rewrote your question to fit this website. \$\endgroup\$ – Philipp Aug 3 '16 at 8:12
  • \$\begingroup\$ Thank you so much for going extra mile than just answering the question, Philipp. \$\endgroup\$ – Bhoopalan Thaati Aug 4 '16 at 3:32
5
\$\begingroup\$

You can change the color of an object with a very simple C# script on the object.

First the quick and dirty solution to make an object red:

GetComponent<Renderer>().material.color = Color.red;

This changes the first color property of the first material of the object to RGB #ff0000. When you would like to set a specific color property of a specific material, check out Renderer.materials and Material.SetColor.

Why is this solution dirty? Because what it actually does internally is to create a complete copy of the material, then change the color value on it and then assign the new material to the object. This is not just slow, it also means that the object now has an own material and no longer shares its material with all other objects which use the same material asset.

A far more elegant solution is to use a material property block:

 MaterialPropertyBlock props = new MaterialPropertyBlock();
 props.AddColor("_Color", Color.red);
 GetComponent<Renderer>().SetPropertyBlock(props);

When you set a property block, the material assigned to the object stays unchanged, but the renderer of this game object overrides its settings with those set on the property block.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thanks again, Philipp. I haven't tried the code yet. I'll check and respond soon. \$\endgroup\$ – Bhoopalan Thaati Aug 4 '16 at 3:32
  • \$\begingroup\$ So how can I do this for multiple materials? I have a mesh with 5 materials and each one should be randomly selected per object. \$\endgroup\$ – mr-matt Nov 25 '16 at 0:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.