how to change image texture using C#

hi,

i have an object with many textures and i want to change every texture with the single press of button, the problem is here i m very beginner of C# and don’t know where to start i really don’t know the basic of scripting, can anybody help or suggest me ??

any help would be appreciated

Thanks,

Hope this helps. I’m hardly the best, but this should work for your purpose. It’ll grab the position, let your cycle through the whole array, and assigns it to your object’s texture.

public Texture[] myTextures = new Texture[5];
int maxTextures;
int arrayPos = 0;

void Start ()
	{
		maxTextures = myTextures.Length-1;
	}


void Update ()
	{
		if (Input.GetKeyDown(KeyCode.U))
		{
			renderer.material.mainTexture = myTextures[arrayPos];

			if(arrayPos == maxTextures)
			{
				arrayPos = 0;
			}
			else
			{
				arrayPos++;
			}

		}  
	}

Just attach the script to the object whose textures you want to change/cycle, and then drag & drop your textures to it in the editor. Doing it this way allows you to reuse this script on any object you want without having to recode anything.

Take note that if you’re really wanting to change out their Material instead of the direct Texture, then you’ll want to create your Material assets and then just change out Texture in this code with Material.

Example: public Material myMaterials = new Material[5];

Here is the code revised to change Materials instead of Textures.

    public Material[] myMaterials = new Material[5];
    int maxMaterials;
    int arrayPos = 0;

    void Start ()
    {
       maxMaterials = myMaterials.Length-1;
    }


    void Update ()
    {
       if (Input.GetKeyDown(KeyCode.U))
       {
         renderer.material = myMaterials[arrayPos];

         if(arrayPos == maxMaterials)
         {
          arrayPos = 0;
         }
         else
         {
          arrayPos++;
         }

       }  

    }

You can basically use this setup for whatever you’re wanting to cycle with, just swap out the types. If you wanted to have a “previous” button, just make another section in the Update for that key and have it arrayPos–; instead.

If your default texture is also the first one in your array, you’ll just want to put the renderer below the if check in update.

	void Update ()
	{
		if (Input.GetKeyDown(KeyCode.U))
		{
			if(arrayPos == maxMaterials)
				arrayPos = 0;
			else
				arrayPos++;

			renderer.material = myMaterials[arrayPos];
		}  

		if (Input.GetKeyDown(KeyCode.Y))
		{			
			if(arrayPos == 0)
				arrayPos = maxMaterials;
			else
				arrayPos--;

			renderer.material = myMaterials[arrayPos];
		}  

	}

You should go check out the Learn Game Development Without Coding Experience | Unity site on their webpage. They’ve been updating it like crazy lately and it has a lot of excellent information there. There is a big section for the scripting also, and it goes over everything you’ll need to know about C# to work with Unity.

C# Unity Scripting:
Learn game development w/ Unity | Courses & tutorials in game design, VR, AR, & Real-time 3D | Unity Learn

Btw, it’s bad practice to do what I just did in the Update (), you should make a method for it and call it in the Update instead. :smiley:

	public Material[] myMaterials = new Material[5];
	int maxMaterials;
	int arrayPos = 0;

	void Start ()
	{
		maxMaterials = myMaterials.Length-1;
	}

	void updateMaterials()
	{
				//Cycle forward
		if (Input.GetKeyDown (KeyCode.U)) {
						if (arrayPos == maxMaterials)
								arrayPos = 0;
						else
								arrayPos++;
			
						renderer.material = myMaterials [arrayPos];
				}


		
				//Cycle backwards
		if (Input.GetKeyDown (KeyCode.Y)) {			
						if (arrayPos == 0)
								arrayPos = maxMaterials;
						else
								arrayPos--;
			
						renderer.material = myMaterials [arrayPos];
				}
	}

	

	void Update ()
	{
		updateMaterials ();
	}

hey ,
you can use this line of code to change your texture,

transform.renderer.material.SetTexture(“_MainTex”,“texture”);

as a texture you can use any texture you wish.