Unity 2D - Dynamic Cube colors are not changed

using UnityEngine;
using System.Collections;

public class RandomCube : MonoBehaviour {

	// Use this for initialization
	void Start () {

		for (int count = 1; count < 4; count++) {
			GameObject cube = GameObject.CreatePrimitive (PrimitiveType.Cube);
			cube.transform.position = new Vector2 ((count - 1) * 6, 0.5F);
			//cube.transform.localScale.x = 5;
			cube.transform.localScale = new Vector2(4, 4);
			cube.GetComponent<Renderer>().material.color = new Color(0, 255 ,255);

			//cube.GetComponent<Renderer>().enabled = true;
	// Update is called once per frame
	void Update () {

	void OnMouseDown() {
		Debug.Log ("clicked");

I created one empty game object and made it as a child for the main camera.
I added a light component

This is script is added as a component to EmptyGameObject

  1. The colors are always black.
  2. The OnMouseDown function is not printing anything
  1. You are using Color with values of 255 while Color uses 0f-1f. Either

a) change the values so they are 0f-1f

cube.GetComponent<Renderer>().material.color = new Color(0, 1, 1);

b) switch to Color32 which uses 0-255

cube.GetComponent<Renderer>().material.color=new Color32(0, 255, 255, 255);
  1. Your z-scale is set to 0, avoid this.

a) If you want a cube (or rectangular prism) then use a cube

GameObject cube=GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.localScale=new Vector3(4, 4, 4);

b) If you want a square (or square) instead of a cube then use a quad instead, i.e.

GameObject cube=GameObject.CreatePrimitive(PrimitiveType.Quad);
cube.transform.localScale=new Vector3(4, 4, 1);
  1. OnMouseDown is called when the user has pressed the mouse button while over the GUIElement or Collider.

a) If you want “clicked” to print while clicking on the gameObject RandomCube is attached to then the gameObject it is attached to needs a GUIElement or Collider

b) If you want “clicked” to print while clicking the mouse anywhere then check if the button is pressed in Update, i.e.

void Update(){