change texture in real time for a gameobject

Hello!
I’m stuck whit this script

#pragma strict
var textureM : Texture;
var textureS : Texture;
var textureD : Texture;
var textureC : Texture;
function Update () {
//MOVEMENT
    if(Input.GetKey("left")) {

    transform.Rotate(0,180*Time.deltaTime,0);
    }
    else if(Input.GetKey("right")) {

    transform.Rotate(0,-180*Time.deltaTime,0);
    }
//END MOVEMENT 

if (Input.GetKey (KeyCode.Q))
renderer.material.mainTexture = textureM;
if (Input.GetKey (KeyCode.W))
renderer.material.mainTexture = textureS;
if (Input.GetKey (KeyCode.E))
renderer.material.mainTexture = textureD;
if (Input.GetKey (KeyCode.R))
renderer.material.mainTexture = textureC;
   
 }

this script is in the inspector of the gameObject
When I press Play and press Q W E or R, Unity change the texture ONLY in the Inspector, but not for the Object in the scene.

How Can I fix this?

Thanks

Did you say scene? Does it change in the game view?

it doesn’t change neither in scene and game view. And the game view is my interest.
sorry I forgot it

I don’t understand how you can see a change in the inspector but not see it in the game view.
You have applied a different texture for each of your texture variables?

here my situation

What we are interested here is the material under Mesh Renderer, and as can be seen from your screenshot it hasn’t changed from matrix.

Instead of changing the texture of the material, have you tried creating 4 different materials, and in the script change the material of the renderer instead of the material.maintexture?

the new script:

#pragma strict
/*var textureM : Texture;
var textureS : Texture;
var textureD : Texture;
var textureC : Texture;
*/
var matM : Material;
var matS : Material;
var matD : Material;
var matC : Material;
function Update () {
//MOVEMENT
    if(Input.GetKey("left")) {

    transform.Rotate(0,180*Time.deltaTime,0);
    }
    else if(Input.GetKey("right")) {

    transform.Rotate(0,-180*Time.deltaTime,0);
    }
//END MOVEMENT 

if (Input.GetKey (KeyCode.Q)){
//this.renderer.material.mainTexture = textureM;
this.renderer.material = matM;
}
if (Input.GetKey (KeyCode.W)){
//this.renderer.material.mainTexture = textureS;
this.renderer.material = matS;
}
if (Input.GetKey (KeyCode.E)){
//this.renderer.material.mainTexture = textureD;
this.renderer.material = matD;
}
if (Input.GetKey (KeyCode.R)){
//this.renderer.material.mainTexture = textureC;
this.renderer.material = matC;

}
   
 }

The result is the same as before. Except for the material, now the name changes.

Oh, huh, that’s so weird. I can’t see anything wrong with that code, but can you check with Debug.Log if those conditions actually pass?
I’ll also give this a try at home in a few hours and see what’s up.

I try with the Debug, and thanks for your help!

I don’t know what’s going on. I tried your code at home with two materials and works fine for me. Is there anything special about the materials you are using? Why does it say (Instance) on them?

I tried with others materials, just for see if the problem is in the materials/textures used.
The (Instance) don’t appears but the problem is the same.

EDIT:

ok, I tried to create a standard cube. Well the script runs perfect…so the problem is my object?
I import him from 3ds Max

OK maybe I found a solution.
My object is a composition of a mesh and a bone (for the movement).
If I apply the script direct to the mesh it works! but not the rotation
so I have to split the script in 2.
One part for the movement to assign at the all object
One part for the material to assign to the mesh inside the object.

Now I try!

Tried and all work now!!!

Thanks for your help