How to replace material in playmode?


Lets say i have a chair in scene. I would like to change its material/shader/texture when in play mode. Maybe to be able to choose from a set of materials. Maybe to also be able to add a video as a material and be able to change it in the same way(as television or projection). Do you know if there is by any chance script that would help? I do not program myself. There is no problem buying the script, I just have not been lucky to find one.

Thank you!

You just need a script. If they are complex shaders, then you’d need to have the materials prepared before hand in the editor. (It’s just easier that way, otherwise you’d need to have a menu selection for the user and then change the material’s normal/texture map when the user selects it.
Put your materials in an array. In the script you’d need to save a reference to the chair’s renderer
and on run time change the material of that reference when the user does something.
To actually make a complete script we’d need to know how exactly you’d interact with it.
(The funcion to change the material will be just a function that you could call from what ever)

Basically you need to have a reference to the material or materials you wanna swap with. To have a reference you can use a public field like this: public Material materialReference; in your script. Then you can assign a material to that field in the inspector. After that, you need to get access to the Renderer component of the object you want to change its material, because that’s the component that takes care of material and the appearance of the gameObject. This is the piece of code you need to get to somehow:

GetComponent<Renderer>().material = materialReference;

and if you just want to change the texture of your material you can do this:

public Texture2D textureReference;

void OnMouseDown() {
    GetComponent<Renderer>().material.mainTexture = textureReference;

How you going to implement your application to run this line of code is up to you and it’s a different story.