Assign a .js a component to a game object from a C# script.

Hi there, I have a C# script I found on unity answers, that does what I needed, it creates an array of cubes. The relevant part from the script is:

    void createLotsOfCubes ()
{
    // Actual renderer size
    GameObject tmpCube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    cubeSize = tmpCube.renderer.bounds.size;
    cubeHalfSize = cubeSize/2f;
    DestroyImmediate(tmpCube);

    // There are nCubes - (1,1,1) spaces between the cubes so the total size is
    Vector3 figureSize = new Vector3( nCubes.x * cubeSize.x + (nCubes.x - 1f) * spaceBetweenCubes.x, 
                                                        nCubes.y * cubeSize.y + (nCubes.y - 1f) * spaceBetweenCubes.y, 
                                                        nCubes.z * cubeSize.z + (nCubes.z - 1f) * spaceBetweenCubes.z );
    // Half of the size of the entire set of cubes
    Vector3 figureHalfSize = figureSize / 2.0f;

    for(int y = 0; y < nCubes.x ; y++) 
    {
         for (int z = 0; z < nCubes.y ; z++)  
        {
            for ( int x = 0; x < nCubes.z ; x++)  
             {
                GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
                // Set this object as the parent object
                cube.transform.parent = transform;
                 // The position is : -extent + size of cube + spacing + offset to center (half size of cube)
                cube.transform.position = new Vector3( -figureHalfSize.x + x*(spaceBetweenCubes.x + cubeSize.x) + cubeHalfSize.x, 
                                                                            -figureHalfSize.y + y*(spaceBetweenCubes.y + cubeSize.y) + cubeHalfSize.y, 
                                                                            -figureHalfSize.z + z*(spaceBetweenCubes.z + cubeSize.z) + cubeHalfSize.z );
                // Change the color
                 cube.transform.renderer.material.color = colors[Random.Range(0, 3)];
            }
         }
    }

And that works fine, it creates all the cubes. Now however I would like each cube that is created to be effected individually by mouse actions, each cube that is created would have this simple transform by the mouse (later I hope to use mouse over, but this is just for illustration purposes).

    var cubeThing : Transform;
    var lastMousePosition : Vector3;

 function Update(){

if(Input.GetMouseButton(0)){
    if(Input.GetMouseButtonDown(0)){
            //reset
            lastMousePosition = Input.mousePosition;        
    }
    else if(Input.mousePosition.y < lastMousePosition.y){
            cubeThing.Rotate(
          Vector3.up*(Input.mousePosition.y - lastMousePosition.y));
    }
    lastMousePosition = Input.mousePosition;
}

Whats the best way to accomplish this? I've tried adding the .js component to a prefab I made out of all the cubes, but that didn't work (half didn't expect it to). So I assume after the third for statement where the cubes are generated, something goes there. Could somebody help me out with what?

Thanks in advance.

Create a single prefab, that includes your .js script and your cube. Then change your createLotsOfCubes method to simply instantiate many of your prefabs.

Everything will then all be nicely wrapped up for you already. And the prefabs will behave as you want them to.