- Home /
How do you change the material of a plane using a C# script?
I want to set a png image as the material of a plane but the thing is that this png image will change every time the user runs the game. I tried simply setting the plane's material to the png image the normal way (dragging and dropping) but it doesn't update every time like I thought it would so I want to run a script when the level is loaded. If someone could point me in the right direction that would be fantastic!
Answer by Jeff-Kesselman · Jun 25, 2014 at 05:09 PM
myPlane.renderer.material.mainTexture = <your texture>
Thanks for the reply, can you explain a bit on how to reference a specific plane in the hierarchy with "myPlane" or maybe link a guide that would explain it?
I'm sorry if this is a dumb question, I'm new to this sort of thing.
If you put this script on the plane then you can just refer to "renderer" as the game object is assumed.
Otherwise you can have a public GameObject field that you drag the plane into in the editor.
Or you can use any of the GameObject.Find... functions to find it at run-time.