Pink texture for in a build program, works while in theditor. Minimal reproducible script included.

Why it is happening? Files should not be missing, I build texture within program.

Imgur: The magic of the Internet (works in editor)

Imgur: The magic of the Internet (fails in a produced executable)

I am on Ubuntu 19.10 (not sure what else can be relevant).

Script is attached to an empty gameobject. If useful I may publish git repo with the entire project.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MapMaker : MonoBehaviour
{
    void Start()
    {
        int size = 100;
        //code from https://stackoverflow.com/questions/27929643/procedural-textures-in-unity
        Texture2D texture = new Texture2D(size, size, TextureFormat.ARGB32, false);
        texture.filterMode = FilterMode.Point;

        for (int x = 0; x < size; x++) {
            for (int y = 0; y < size; y++) {
                Color color = Color.blue;
                if(x<10) {
                    color = Color.red;
                }
                if(y<10) {
                    color = Color.green;
                }
                texture.SetPixel(x, y, color);
            }
        }
        texture.Apply();
        //end of copied code

        GameObject screen = GameObject.CreatePrimitive(PrimitiveType.Quad);
        screen.transform.name = "screen";
        screen.transform.position = new Vector3(0, 0, 1);
        screen.transform.localScale = new Vector3(10, 10, 1);
        screen.GetComponent<Renderer>().material.mainTexture = texture;      
    }
}

If anyone has even idea how to debug this help would be welcomed! I am really confused by what is happening here.

Unity could be stripping the default shader out because you don’t have a reference to it.

Drag a quad or cube into your scene, then disable it, save the scene and build. Did that fix it?

3 Likes

Thank you! Now everything worked!

1 Like

Kurts solution worked for me, looks like if you have no objects at the scene unity doesnt build good :expressionless:

1 Like

Yeah, basically the resources in Unity are gathered and chosen to include based on actual scene references plus the Resources directory, not merely based on the fact that some piece of code such as this one:

might need the default shader to make that cube.

I see why they chose this path, but I still think it’s a bit silly; it seems certain small default stuff should just be immune to stripping, because it can’t really be saving more than a few bytes!

But hey, minor complaint, and it’s easy to fix… once you know what it is! :slight_smile: