Changing Object Texture When button is pressed.

i found a script, which when a button is pressed, the texture of an object changes:

var texture1:Texture2D;

function Update () { if(Input.GetButton("Forward") ) {

  if(texture1) 
  { 
     Debug.Log("Texture Loaded Sucessfully..."); 
     renderer.material.mainTexture = Texture; 
  } 

  else 
  { 
     Debug.Log("Unable to Load texture..."); 
  } 
 } 
}

however i need this feature, but more than one texture on more than one button. I made the following changes:

var texture1:Texture2D;

var texture2:Texture2D; var texture3:Texture2D; var texture4:Texture2D;

function Update () { if(Input.GetButton("Forward") ) {

  if(texture1) 
  { 
     Debug.Log("Texture Loaded Sucessfully..."); 
     renderer.material.mainTexture = Texture; 
  } 

  else 
  { 
     Debug.Log("Unable to Load texture..."); 
  } 
 } 
 if(Input.GetButton("Backward") ) 

{

  if(texture2) 
  { 
     Debug.Log("Texture Loaded Sucessfully..."); 
     renderer.material.mainTexture = Texture; 
  } 

  else 
  { 
     Debug.Log("Unable to Load texture..."); 
  } 
 } 
 if(Input.GetButton("Left") ) 

{

  if(texture3) 
  { 
     Debug.Log("Texture Loaded Sucessfully..."); 
     renderer.material.mainTexture = Texture; 
  } 

  else 
  { 
     Debug.Log("Unable to Load texture..."); 
  } 
 } 
 if(Input.GetButton("Right") ) 

{

  if(texture4) 
  { 
     Debug.Log("Texture Loaded Sucessfully..."); 
     renderer.material.mainTexture = Texture; 
  } 

  else 
  { 
     Debug.Log("Unable to Load texture..."); 
  } 
 } 
}

however this doesnt work properly. Any ideas how i can fix this?

Well, I think this has been answered a few times here, and efge's answer is one simple way of doing it. Here is another example (basically the same as efge's):

// Cache our renderer
private var rend : Renderer;

// Store the textures in an array. Then you can quickly and more or remove some
public var textures : Texture2D[];

// Awake
function Awake()
{
  // Get the renderer
  rend = gameObject.GetComponent(Renderer);

  // If none, then add one
  if (rend == null)
    rend = gameObject.AddComponent(MeshRenderer);
}

// Update function
function Update()
{
  if (Input.GetButton("Forward"))
    ChangeTexture(0);
  else if (Input.GetButton("Backward"))
    ChangeTexture(1);
  else if (Input.GetButton("Left"))
    ChangeTexture(2);
  else if (Input.GetButton("Right"))
    ChangeTexture(3);
}

// Change texture
function ChangeTexture(inx : int)
{
  // Just make a small check, that we don't try to take a non-existing texture
  if (inx >= textures.Length)
    return;

  // Change texture
  rend.material.mainTexture = textures[inx];
}

You have to assign the right var, e.g.:

renderer.material.mainTexture = texture1; 

(not renderer.material.mainTexture = Texture; )


And maybe it would be better to make use of arrays, like this:

var MyTextures : Textrue2D[];

...

for (i=0; i< MyTextures.Length, i++) 
  if (Texture*)*
 *Debug.Log("Texture" + i " Loaded Sucessfully...");*
*switch (Input.GetButton) {*
 *case : "Backward";*
 *renderer.material.mainTexture = Texture[0];*
 *break;*
 *case : ...*
*```*

Hey, is there a way to make this for materials and not textures