I want to get the object color, when I start the game. How do I do this?

enter image description here

public Renderer cube;
public float r,b,g,a;

void Start() {
            r = cube.GetComponent<Renderer>().material.color =
            b = cube.GetComponent<Renderer>().material.color =
            g = cube.GetComponent<Renderer>().material.color =
            a = cube.GetComponent<Renderer>().material.color =

  • \$\begingroup\$ Did you try grabbing color.r, color.g, etc? \$\endgroup\$
    – Gnemlock
    Dec 25, 2016 at 6:29
  • \$\begingroup\$ We solve this, check please "Shuvro Sarkar" answer. Thank you. \$\endgroup\$
    – user43474
    Jan 7, 2017 at 17:55

1 Answer 1


Its actually simple. Just use Color32 class to get the r, b, g, a of your desired gameobject. Here is an Example Code. Here I used a cube gameobject and got the color of its material.

public class FirstScript : MonoBehaviour 
// Use this for initialization
  void Start () 
    Color32 objColor;
    objColor = gameObject.GetComponent<MeshRenderer>().material.color;

    print(objColor.r + " "+ objColor.g + " "+ objColor.b+ " "+ objColor.a + " ");


  • 1
    \$\begingroup\$ This answer is correct for the most simple case, but sometimes you have an object with multiple materials. Then you need to pick the right one from the renderer.materials array. Also, your material might use a more complex shader which has more than one color. Then you need to use the material.GetColor method to get the color you want. \$\endgroup\$
    – Philipp
    Dec 25, 2016 at 10:37
  • \$\begingroup\$ In Order to get the color on the screen one could also use render texture with pixel get access and use worldtoscreenpoint to get the pixel positions of the object. Then the exact pixel color can be extracted. However this will also introduce shadows etc.. But in some more sophisticated cases (where simple material access would yield wrong results) this could work. \$\endgroup\$
    – M156
    Jan 18, 2017 at 7:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .