- Home /
Material Cloning Issues
Alright, what I'm trying to do, I've done before in the past long ago. Can't remember how it's done properly. There are many threads on here addressing my issue, but none of the solutions are working for me.
I'm using GetPixels and SetPixels to draw a decal right on the desired texture/material. However, in my case using a vehicle, all 8 tires use the same material/texture. So a decal drawn on one, draws on them all. Which I expected. I tried many solutions to instantiate a unique material/texture for each tire (or any other part for that matter) with no luck.
The following is the code currently (which as you can see is trying very hard to make the new tex/mat unique). It didn't used to be that convoluted, but after each help post I read, it slowly became that way.
code: http://paste.ofcode.org/q9YmKuMG3KXq9JHiRMxZFP
With the current code, nothing shows up at all, with original code it behaved the exact way I'm attempting to prevent.
Attached is a screen shot showing the original issue. (only one tire was actually clicked on).
Your help is much appreciated.
Answer by robertbu · Feb 07, 2014 at 08:36 PM
If you want a unique texture for each tire generated at runtime, in the Start() on a script attached to each tire do:
renderer.material.mainTexture = Instantiate(renderer.material.mainTexture) as Texture;
In unity, if you make any runtime changes to a material, Unity generates a new material instance. The line of code above makes a copy of the texture, so each tire will have both a unique material and a unique texture.
Sorry I didn't reply for two days. I posted this from work and don't have access to work accounts from home.
That's precisely what I've been trying, except I was doing it right before drawing on the texture/material. It didn't occur to me to try in the awake or start. Will give that a try. Will let you know how it works out.
Thanks! That did it! I was trying to duplicate everything but what needed to be. I didn't put the line in Start though. Just the line right before I set my target texture. The script isn't running on the object being drawn on, I'm accessing it via the RaycastHit.
The potential problem with the RaycastHit method is that you will be instantiating a new texture on every hit. I suppose you could keep some boolean values around to track what tires have been updated, or you could change the name or the tag after they've been updated. You only need to do this once. After the initial change, all the tires will have a unique material with a unique texture.
Indeed, I've implemented a tracking system, so each texture is only ever duplicated once. It also serves to restore the original textures OnApplicationQuit.
Your answer
![](https://koobas.hobune.stream/wayback/20220613132837im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can I set the material on an instance after creating it? 1 Answer
How to Make a Character Flicker? 1 Answer
Changing one material instance affects all instances? 4 Answers
Would a color based game run better than a texture based game? 2 Answers
Attaching scripts to meshes with multiple materials 2 Answers