- Home /
Texture swapped out works in editor but not in build
So I've got an object (a ceiling in a room) that I swap the texture on at the press of a button.
var mats = Resources.FindObjectsOfTypeAll(typeof(Material));
if (Input.GetKeyDown(KeyCode.KeypadEnter) == true)
{ foreach (Material mat in mats)
{ if ((mat.name == "ceiling_tiled"))
{ ceilingMeshRenderer.material.SetTexture("_MainTex", mat.mainTexture);
}
}
}
(There's almost certainly a much better way to select and assign the texture, but hey, I'm learning, and it worked...)
I've got a set of cubes in the scene outside of the visible/playable area with these textures on them, so that they load fine when the game loads. (again, probably a better way to do this, but I couldn't get the manual resource loading to work so I did this instead.) In the editor the above code works fine and it swaps the texture on the ceiling just fine.
However after I build, sometimes it doesn't swap the texture. About 4 out of 5 builds, the texture swapping refuses to work and the "ceiling_tiled" texture does not display. I have absolutely no idea what I'm doing differently on the 5th build, in fact I'm 99% sure I'm not doing anything different at all. I've tried copying the material and texture files into the Assets/Resources folder, no dice.
I'm going bonkers trying to figure this out. Please help.
edit: now using a more intelligent way to address the GameObject and texture rather than just cycling through them all to find the one I want... still does the same thing, Built version doesn't change texture but when played in Editor it works fine.
Answer by samuelplattwge · Aug 28, 2018 at 11:49 PM
After 2 weeks of wrestling with this, I spent 2 days recreating the scene in a new project. Didn't change a thing. Reimported all assets and scripts. Sacrificed a goat. Worked first go.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Initialising List array for use in a custom Editor 1 Answer
Changing HDRP Material's Texture At Runtime Not Working? 2 Answers
Code to work in editor vs builds 1 Answer