- Home /
Highlighting multiple sprites automatically changes all their materials in Unity 5?!
If I literally just click and drag round some sprites to move them all, they instantly all change to sharing one material?!
How can I stop this behaviour? (5.1.2f1 for PS4)
Answer by ScienceIsAwesome · Jul 24, 2015 at 03:44 PM
Wow, that is a ridiculous bug :P
I can't believe it but the only thing I could come up with is this:
using UnityEngine;
[ExecuteInEditMode]
public class MaterialForcer : MonoBehaviour
{
public Material forcedMaterial;
void Update()
{
RefreshMaterial();
}
public void RefreshMaterial ()
{
GetComponent<Renderer>().sharedMaterial = forcedMaterial;
}
}
Put that on every sprite and put the desired material in forcedMaterial. This will work both in play and edit mode and won't be necessary anymore once you've finished the game and are ready to build, just remember to remove it then. If you have a bunch of sprites and notice a drop in performance, you can make them refresh less often with a counter or something in play mode only, something like:
int counter = 0;
void Update()
{
if(Application.isPlaying)
{
if(counter > 9)
{
RefreshMaterial();
counter = 0;
}
else
{
counter++;
}
}
else
{
RefreshMaterial();
}
}
When you've finished the project you can run something like this to clean up, coming out of play mode once with this on a gameobject is enough:
using UnityEngine;
[ExecuteInEditMode]
public class Suicide : MonoBehaviour
{
void Start ()
{
foreach (MaterialForcer materialForcer in FindObjectsOfType<MaterialForcer>())
{
DestroyImmediate(materialForcer);
}
}
}
Someone should put this bug in the issue tracker, if it's not there already
Cheers. I could force the material but it seems so dumb that I shouldn't have to.
It's in the issue tracker, voted for it:
Your answer
![](https://koobas.hobune.stream/wayback/20220611232838im_/https://answers.unity.com/themes/thub/images/avi.jpg)