How to change a material's texture via script with a .PNG?
Hi guys, I am sure this might be a dumb question, but I am kind of a noob with Unity and I really need help for this university project!
I am making a simple racing cars game and I am trying to create a selection menu to change the texture of the car. Now, I got the car from the asset store and it has a material attached to it, so I'm trying to change the texture of the material from script WITHOUT changing the material itself, since I need to retrieve a .PNG from a database and use it as my new texture. How can I apply the change to the material? What I try to use is the following code, and I'm just trying to make it work without the connection to the database, selecting the texture as a public Texture from the Unity inspector:
public Texture txtr;
void Update () {
Renderer renderer = GetComponent<Renderer> ();
Material mat = renderer.material;
mat.SetTexture("_MainTxt", txtr);
}
but I can't seem to make it work. Why is that the case? What am I doing wrong? Thanks in advance!
Your answer
![](https://koobas.hobune.stream/wayback/20220612124452im_/https://answers.unity.com/themes/thub/images/avi.jpg)