- Home /
Copy one texture from another gameObject
How would you copy one texture from 1 game object to another?
I tried this and it did not work.
GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane);
plane.renderer.material.mainTexture = GameObject.Find"testObject").render.material.mainTexture;
I get message saying it does not contain a definition for render and no extension method.
I like to get the testObject texture copied to the plane texture via script automatically.
Answer by d112570 · Apr 16, 2014 at 03:37 PM
This worked
plane.renderer.material.mainTexture = GameObject.Find
("TestObject").renderer.material.mainTexture;
I tried many version like.
plane.renderer.material.Texture = GameObject.Find("TestObject").renderer.material.Texture;
plane.renderer.material.Texture = GameObject.Find("TestObject").renderer.material.mainTexture, Texture;
planet.Renderer.Material.Texture = GameObject.Find("TestObject").Renderer.Material.Texture;
plane.renderer.material.mainTexture = GameObject.Find"testObject").render.material.mainTexture;
I had no idea I was this close. The answer was this.
plane.renderer.material.mainTexture = GameObject.Find("TestObject").renderer.material.mainTexture;
Answer by NickP_2 · Apr 16, 2014 at 02:51 PM
Depends if the shader has a maintexture, to be sure, go to the shader and chech for something like _Mat declared on top. Keep that name in the back of your mind because you can get the texture of a shader with this:
renderer.material.GetTexture("<texture name>");
this works for normalmaps, diffuse, just any texture the shader asks!
EDIT:
Oh yea, and you can set it with:
renderer.material.SetTexture("<texture name>", Texture);
plane.renderer.material.mainTexture = renderer.material.SetTexture(GameObject.Find("TestObject"), Texture);
Is this approach wrong? It did not work.
also tried
plane.renderer.material.SetTexture(GameObject.Find("testObject").renderer.material.mainTexture, Texture);
}
Yes its wrong, take a look here, you need to use the name in the shader script
Your answer
![](https://koobas.hobune.stream/wayback/20220613142844im_/https://answers.unity.com/themes/thub/images/avi.jpg)