Changing Texture With a Click of Mouse Button

I have a problem with my coding. To me the codes below is correct and make sense :sweat_smile: .
Unfortunately it does not work very well. The script does not change as what I expected.

My expectation was

  1. When a button is clicked.
  2. The button will execute an external script which will change the texture of another 3D Object.
using UnityEngine;
using System.Collections;

public class Calling : MonoBehaviour {
	int i = 0;
	//Create an Object reference named switchImage which is refereing to the SwitchImage Class.
	SwitchImage switchImage=new SwitchImage();
		
		// Update is called once per frame
		public void OnMouseDown () {
			switchImage.Update();
		}
}
using UnityEngine;
using System.Collections;
using System;

public class SwitchImage: MonoBehaviour{

	public Texture diffuseTexture0;//Textures 0
	public Texture diffuseTexture1;//Textures 1
	public Texture diffuseTexture2;//Textures 2
	
	//Texture Size
	static int textureSize = 3;
	
	public int i = 0;	
	
	private bool start = false;	
	
	public void Update()
	{
		Texture[] textureArray = new Texture[textureSize];
		textureArray[0] = diffuseTexture0;
		textureArray[1] = diffuseTexture1;
		textureArray[2] = diffuseTexture2;
			
		//set the texture according to the array
		renderer.material.SetTexture("_MainTex", textureArray[i]);
		
		//Increment the i Variable.
		i++;
		
		//Check whether the value of i is equal or bigger than the value of textureArray.
		//If yes, then set i = 0
		if(i>=textureSize){
			i=0;
		}
	}
}

I know it’s Friday guys. Please help me guys.

Thanks :smile:

It’s a problem you call your function “Update” as that’s a built-in function name, used to do stuff each frame.
Try calling it “DoTextureSwitch” or something like that. Right now I would guess your image just switches once per frame;)

Btw, your code is a little messy, and not very optimized. Here is some ideas for improvements…

using UnityEngine; 
using System.Collections; 
using System; 

public class SwitchImage: MonoBehaviour{ 

   public Texture2D[] diffuseTextures;
   public int indexPointer = 0;    
    
   //What does this do...?
   private bool start = false;    
    
   public void DoTextureSwitch() 
   { 
              
      //set the texture according to the array 
      renderer.material.SetTexture("_MainTex", diffuseTextures[indexPointer]); 
       
      //Increment the i Variable. 
      indexPointer++; 
       
      //Check whether the value of indexPointer is equal or bigger than the length of diffuseTextures. 
      //If yes, then set indexPointer = 0 
      if(indexPointer >= diffuseTextures.Length){ 
         indexPointer = 0; 
      } 
   } 
}

Thanks for the response, i will try the code ASAP.

:smile:

using UnityEngine; 
using System.Collections; 
using System; 

public class SwitchImage: MonoBehaviour{ 

   public Texture2D[] diffuseTextures;
   public int indexPointer = 0;    
    
   //This is just part of my old code :sweat_smile:
   private bool start = false;    
    
   public void DoTextureSwitch() 
   { 
              
      //set the texture according to the array 
      renderer.material.SetTexture("_MainTex", diffuseTextures[indexPointer]); 
       
      //Increment the i Variable. 
      indexPointer++; 
       
      //Check whether the value of indexPointer is equal or bigger than the length of diffuseTextures. 
      //If yes, then set indexPointer = 0 
      if(indexPointer >= diffuseTextures.Length){ 
         indexPointer = 0; 
      } 
   } 
}

[/quote]
:smile:

Ach sooo…

Thanks. I will do that also.

I will give you an update.

:stuck_out_tongue:

What I am hoping for is that

  1. The texture change when another object is clicked.

For example
When you click on a Square object, it will imediately or triggered changes the texture of a Plane object.

But it still does not work :sweat_smile:

The code below is the trigger. This will be attached to a square object.

using UnityEngine;
using System.Collections;

public class Calling : MonoBehaviour {
	//Create an Object reference named switchImage
	
SwitchImage switchImage=new SwitchImage();
		
		// Update is called once per frame
		public void OnMouseDown () {
			switchImage.DoTextureSwitch();
		}
}

The code below attached to the plane object which will be affected the the code above. :smile:

using UnityEngine;
using System.Collections;
using System;

public class SwitchImage: MonoBehaviour{

   public Texture2D[] diffuseTextures;
   public int indexPointer = 0;   
   
   public void DoTextureSwitch()
   {
             
      //set the texture according to the array
      renderer.material.SetTexture("_MainTex", diffuseTextures[indexPointer]);
       
      //Increment the i Variable.
      indexPointer++;
       
      //Check whether the value of indexPointer is equal or bigger than the length of diffuseTextures.
      //If yes, then set indexPointer = 0
      if(indexPointer >= diffuseTextures.Length){
         indexPointer = 0;
      }
   }
}

Error

no no no not once per frame. But changes once per clicked :wink:

I know your intentions, I’m just saying your code would make it switch once per frame… :slight_smile:

Thanks for the respond Kragh :smile:

The code seems alright, but Unity3D does not understand the code. Hmmmmmmmmm :?:

Do you have any idea why?

Do you mean you are getting error messages? Please can you post them if so.

Hi,

I am also facing the same problem were the texture is not changed.
can anyone help me out.

Thanku