- Home /
How to replace material in playmode?
Hello,
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!
Matej
Do you need a script for changing an object's material from a pre-set material to another pre-set material, or do you need to create a new material altogether?
Just change pre-set material, and to be able to pick from a range of different ones. Thank you!
If you want to play a movie, try taking a look at this: https://www.youtube.com/watch?v=fQH2hH9Abdo
What do you mean by "in playmode"? Do you mean change the material at runtime via script? Or do you mean changing a material in playmode and keep the change after stopping the playmode?
Thanks for the film!
I mean something like this here: https://www.youtube.com/watch?v=WyAg0h7uxv8 at 1.15$$anonymous$$
Answer by screenname_taken · Nov 06, 2015 at 12:15 PM
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)
Answer by ben-rasooli · Nov 09, 2015 at 01:42 PM
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.
This is an example:
using UnityEngine;
public class $$anonymous$$aterialChanger : $$anonymous$$onoBehaviour {
public $$anonymous$$aterial first$$anonymous$$aterialReference; //assign via inspector.
public $$anonymous$$aterial second$$anonymous$$aterialReference; //assign via inspector.
void Start()
{
GetComponent<Renderer>().material = first$$anonymous$$aterialReference;
}
void On$$anonymous$$ouseDown ()
{
GetComponent<Renderer>().material = second$$anonymous$$aterialReference;
}
void On$$anonymous$$ouseUp ()
{
GetComponent<Renderer>().material = first$$anonymous$$aterialReference;
}
}
Attach this script to the gameObject you wanna change its material and then assign two materials to those two public fields via the inspector.
Your answer
![](https://koobas.hobune.stream/wayback/20220612044035im_/https://answers.unity.com/themes/thub/images/avi.jpg)