Adding a texture to a Primitive Cube

hello i basically got bored so i looked around for a minecraft type script and came across a Unity package and it was all there for you, but i dunno how to add a texture to the PrimitiveType.Cube, i tryed everything i know and it didnt work all i want is the choice to add a texture to the cube i create, it would be added multiple time in the scene this is why i need it to be in the code, here is the code

var blockLayer : LayerMask = 1; 

var range : float = Mathf.Infinity; 

var hit : RaycastHit;

var texture : Texture;

function Update () { 

    if (Input.GetMouseButtonDown(0)) Build(); 

    if (Input.GetMouseButtonDown(1)) Erase(); 
}

function Build() { 

    if (HitBlock()) { 

    var cube = GameObject.CreatePrimitive(PrimitiveType.Cube); 

    cube.transform.position = hit.transform.position + hit.normal; 

    } 

}

function Erase() { 

    if (HitBlock()) 

    Destroy(hit.transform.gameObject); 

}

function HitBlock() : boolean { 

     return Physics.Raycast(transform.position, transform.forward, hit, range, blockLayer); 

}

Thanks.

Maybe if you create a Texture2D variable and set a texture in inspector you should be able do add it to your primitive with:

public var yourTexture: Texture2D;

[...]

cube.renderer.material.mainTexture = (Texture)yourTexture;