New to Unity - create coloured cubes

Hi there !

I’m new to Unity and I have to create a very simple scene. Here is a description :
3 transparent cubes and a different colour for each cube… that’s all !

I don’t need the cubes to change colour when we click on them but I need them to be transparent.

What did I try before asking this ?

Well, when I change the colour of a cube it actually changes the colour of all the cubes in my scene. (all the cubes are children of the same ImageTarget) I tried to create a new script and attached a different script to the different cubes. Here is the general content of the scripts :

using UnityEngine;
using System.Collections;

public class colourScript : MonoBehaviour {

	// Use this for initialization
	void Start () {
		renderer.material.shader = Shader.Find ("Transparent/Diffuse");
		renderer.material.color = new Color(0.5f,0.3f,0.2f,168);
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

The problems are that …

1.Transparency

My cube does not seem to be transparent as I need it to be. So I think that I am missing something but I have no idea of how to solve this problem…

2.RGB Colors

Moreover, I am using RGB to define the colors… so how can I convert the RGB numbers (range : 0 to 255) into float in the range [0,1] to use it as input of the Color function ?

Or maybe there is an easier way to get the result I want ?

Well, I hope that someone can help me :slight_smile:
Thank you !

I spotted the problem. You were using the RGB scale for the alpha too, just try to think of the alpha as 0 is completely transparent and 1 is completely solid.

If you create your variables as public you will see them appear in your objects hierarchy and change them as the game plays until you find something you’re happy with (make a note of them while you’re in play mode though, because once you exit they’ll revert back to their original values)

Here are the variables for you

public float r;
public float g;
public float b;
public float a;

Now, use RGB as you’re used to, this SHOULD convert it, just change the renderer.material.color line to this and it should work.

renderer.material.color = new Color(r/255, g/255, b/255, a);

Hope this helps