Primitive Cube Texturing Help

I cannot find out how to add a texture to a primitive cube. i have tried but every other way doesent work

Code:

for (var y = -30; y < 5; ++y)
for (var x = -30; x < 5; ++x) {
    var cube : GameObject = GameObject.CreatePrimitive (PrimitiveType.Cube);
    cube.transform.position = transform.position + Vector3(x, 0, y);
	cube.renderer.material.mainTexture ("dirt.png");
	
}

I’m not sure what shader CreatePrimitive gives to your object, does it have a _MainTexture parameter ?

Also, is your dirt.png in the Resources folder ?

im a bit of a noob at coding so can you help me?

I might be wrong but I think that the default material assigned to a primitve is read-only. You you try creating your own material with a proper shader.