[C#] Apply a simple Texture on a plane.

Hello,
i’m trying to load and apply a simple texture on my plane. I’m using C#.

Texture2D p = new Texture2D(10, 10);
		p = Resources.Load("Grass", typeof(Texture2D)) as Texture2D;
		Debug.Log(p);
		mGfxGameGrid.renderer.material.mainTexture = p;

But dosen’t work. So anyone can explain me the way to load a texture (or material) and assign Its to my plane? I’m sorry for this noob question, but i can’t understand how can i solve it from the documentation.

Thanks a lot and sorry for my english!

I think that in line where:

mGfxGameGrid.renderer.material.mainTexture = p;

you can exclude mGfxGameGrid.

Or you can try this:

public Texture mytexture;
void Start()
{
   renderer.material.mainTexture = mytexture;
}

and don’t forget to add your texture in inspector!

if it is the standart Grass.psd your are using then try to make it find another texture because this works for me

Texture2D p =  (Texture2D) Resources.Load("glass", typeof(Texture2D));

the glass is a .tif image. I don’t know if thats the problem

i just noticed that it work with glass because it was in the folder Assets\Resources and when i moved Grass in there to i also worked.

I’ve tried to use “glass” but it still don’t work… Now i will show you my entire class:

using UnityEngine;
using System.Collections;

/*
	Classe che si occupa solo della gestione grafica
	
*/
public class GfxGameGrid
{
	/*
		Class Members
	*/
	private GameObject 	mGfxGameGrid;	 		/* La Gfx della Matrice */
	private Vector3 	mGfxGameGridPosition;	/* La posizione della Matrice */
	private Vector3		mGfxGameGridScale;		/* La grandezza della Matrice */
	
	/*
		Class Methods
	*/
	
	public void initGfxGameGrid(Vector3 position, Vector3 scale)
	{
		Debug.Log("Creating GamePlane..");
		mGfxGameGridPosition 	= position;
		mGfxGameGridScale 		= scale;
		
		mGfxGameGrid = GameObject.CreatePrimitive(PrimitiveType.Plane);
		
		mGfxGameGrid.transform.position 	= mGfxGameGridPosition;
		mGfxGameGrid.transform.localScale	= mGfxGameGridScale;
		//Texture t = new Texture();
		//mGfxGameGrid.renderer.material.SetTexture("Diffuse", t); 
		
		
		Texture2D p = new Texture2D(10, 10);
		p = Resources.Load("glass", typeof(Texture2D)) as Texture2D;
		Debug.Log(p);
		mGfxGameGrid.renderer.material.mainTexture = p;
		
		Debug.Log("Created GamePlane");
	}
}

Thanks![/code]

Try to create a folder in the root “Assets” folder called “Resources” and then move the texture files there. Thats make the code work for me.