Getting pink textures in build

Hi, In my game I’m attempting change materials on an object but am running into a strange issue. When I run the game inside the editor everything works as expected, however, when I make a build, all objects using the Toon/Basic Outline shader (part of the effects package in standard assets) turn bright pink. I’ve noticed that the build will work correctly if I only include one object that has the material changing script attached to it.

Here is the material changing script, it’s loosely based off the Render.material example provided at Unity - Scripting API: Renderer.material :

public class MaterialManager : MonoBehaviour
{
    public Material normalGhostMaterial;
    public Material lightGhostMaterial;
    public Material darkGhostMaterial;
    public float runForSeconds = 5;

    private Renderer rend;
    private float timer = 0;
    private float blinkInterval = 0.4f;
    private float lastBlinkTime = 0f;
    private int blinker = 1;

    void Start()
    {
        rend = GetComponent<Renderer>();
        rend.enabled = true;
    }

    void Update()
    {
        checkTimesUp();       
    }

    public void startRunning()
    {
        rend.material = darkGhostMaterial;
        startTimer();
    }

    void startTimer()
    {
        timer = Time.time;
    }

    void checkTimesUp()
    {
        if (Time.time - timer > runForSeconds)
        {
            rend.material = normalGhostMaterial;
            blinker = 0;
        }
        else
        {
            if (Time.time - timer > 2.5f && Time.time - timer < runForSeconds && Time.time > 5)
            {
                blink();
            }
        }
    }

    void blink()
    {
        if (blinkTimeChange())
        {
            blinker++;
        }
        if(blinker % 2 == 0)
        {
            rend.material = lightGhostMaterial;
        }
        else
        {
            rend.material = darkGhostMaterial;
        }
    }

    void startBlinkTime()
    {
        lastBlinkTime = Time.time;
    }

    bool blinkTimeChange()
    {
        if (Time.time - lastBlinkTime > blinkInterval)
        {
            startBlinkTime();
            return true;            
        }
        else
        {
            return false;
        }
    }
}

I noticed in the sample on the render.material page that they use rend.sharedMaterial instead of rend.material. I tried this but it didn’t fix anything. I’ve also tried the script with the Unlit/Color shader and it seems to work fine with that, but I’d still prefer to use the toon shader. At the moment, the materials I’ve created (with the issues) use a ‘Toon/Basic Outline’ shader with just the ‘Main’ and ‘Outline’ colors defined. There are options for 2D textures but I’ve left them set at ‘None’ (which has been working fine up till this issue). Anyway, as always, thanks for your time and words of wisdom!

This is expected behaviour - when compiling a build, Unity only includes those shaders that it knows are required (i.e. are assigned to a material in the scene). If you want to change a shader programmatically at runtime, Unity won’t know to include that shader unless you explicitly tell it to.

To resolve this, go to Edit → Project Settings → Graphics and add the Toon/Basic Outline shader to the list of Always Included Shaders.

43803-toon.jpg

Edit/ProjectSettings/Graphics. And Right Click at the top of the inspector page for built-in shaders and choose reset it should fix it for you automatically.

The problem is most likely because of how Unity trims the fat when you build projects. Any asset which does not appear to be used will be left out, resulting in “Problem Pink” in the case of missing shaders, materials, or textures. If your only reference to them is in scripting, it won’t detect that they’re needed for the build.

Try putting all these special materials in the Resources folder. OR, an alternative that seems to work is, keep some deactivated dummy objects in your scene that use these special materials so Unity knows to include them in the build.

Tanoshimi you are the man! Your suggestion appears to have resolved the issue. A big thanks to AlwaysSunny as well. I attempted the dummy object approach but it still wasn’t working for me. (I was probably doing it wrong). Anyway, thanks again to both you!

My issue was that all the graphics was missing the sprites default shaders.
I didn’t do anything special, but for some reason when I created a new project and then reimported my asset, the sprite shaders were missing.

Using URP, when create object and set color through code this help me Fix it:

YourGameObject.GetComponent<Renderer>().material.shader = Shader.Find("Universal Render Pipeline/Lit");