Assigning a texture inside a script

So i have a GameObject thats pretty much just a box primitive made inside of unity, but at the objects creation I want to assign a random texture to out of like 40 textures. Making the randomness happen i know how to do but i cant seem to get it to assign a actual texture. this is what im currently trying inside of a script component attached to the game object. I am using C#.

void Start () {
this.renderer.material.mainTexture = (Texture)Resources.Load(“3D.jpg”);
}

Thanks for the assistance!

I would say on start call for a random range, then say if =1 load this marterial, if 2 then other material and so on,

I usually use java script so I can’t post something exact, but the theory is the same

Check the Resources.Load documentation. You shouldn’t use file extensions (you’re not loading the same file you put in the assets folder, but its imported equivalent). Also make sure that whatever you’re loading is in the Resources folder.

Ah I think you have given me the clue I need Jasper, I just had my textures in a folder in my project not in any special Resources folder or section. I am not at home at the moment but when i get there ill definitely see if i can figure the Resources folder thing out.

You could also create a script that stores all of your textures and then reference that script in your script that loads it. That way you don’t have to do any loading during the game, it’s all front loaded.

so in Script 1:

public Texture texture1;

then in script 2:

private Script1 ptrScript1;
private Texture loadedTexture;

then in the Start method (or wherever):

loadedTexture = ptrScript1.texture1;