- Home /
Texture appears Pink on Device (works on editor)
Im trying to apply a simple texture to a Gameobject (plane) that i have created during runtime. Im choosing a simple jpeg image stored in my "Resources" folder, loading it and trying to apply it as a texture.
Heres the following code snippet in my Gameobject class:
Texture2D staticTex = (Texture2D)Resources.Load("test");
Debug.Log("ULog Texture Size Width " + staticTex.width + "ULog texture Size Height " + staticTex.height);
MeshRenderer renderer = plane.AddComponent(typeof(MeshRenderer)) as MeshRenderer;
renderer.material.mainTexture = staticTex;
This works fine on the editor, but i get a pink texture on the iPhone. Im using: Unity : 5.5.0f3 Xcode : 8.0 iOS : 10.0.2
This used to work as expected in previous builds but has just stopped working now.
I have tried:
Clearing the GI cache
Making sure shaders are included by adding a test shader.
Not sure what is going wrong, or what has changed. Any help is appreciated!
Update: The behavior is the same on an android device as well. Just tested with a galaxy s7!
Hi, I've ran into a similar problem before (pink textures on device) and it related to the compression settings for the texture. Hope that helps!
@herDev, thanks for the reply! What is the recommended compression setting for android and iOS?
Answer by GambitDev · Feb 19, 2017 at 10:56 PM
After a few days of trying things i finally got the solution to this. It was an issue with unity not finding/compiling the shader for any of the builds. I just had to add a dummy material in my "Resources" folder (doesnt need to be used) and unity picks this up and compiles the shader for the build.
This happened as a result of me re-organizing the folder structure in my project. I organized all my scripts, assets in my unity project and created a "Resources" folder where i add textures and Prefabs. At this point i had no materials here as all my materials were being created and added at run time. Somehow unity considered this is the project not utilizing any materials and that it was not required to compile any shader.
Adding a dummy shader in my "Resources" folder fixed this issue. Now everything works as expected.
This link http://answers.unity3d.com/questions/1147277/textures-from-local-works-in-editor-missing-in-bui.html gave me the solution for my problem. Hope this helps anyone in the future facing the same issue.
Had the same issue. But I was creating material by Shader.Find and it appeared not working because I deleted some om UNUSED materials from project folder.
Your solution works good. Now I am creating a material instance directly from resource folder.
I just transferred all my materials to "Resources" folder and in worked for me, thanks!
Answer by danilocjr · Mar 23, 2020 at 04:32 AM
A simple way to do this is:
Go to >> Project Settings >> Graphics >> Always Included Shaders
Add 1 in size (to add another element)
Select the Shader you're going to or using it
This way you're going to compile all needed shaders for your build. Don't forget to select the correct platform (iOS/Android/Standalone)
This, as well as the other answers, worked for me. I feel like this is the 'cleanest' approach to the problem.
Answer by himichelleng · Apr 10, 2018 at 04:58 AM
Thank you for giving me a clue to solve a similar problem. In my case, I was trying to add a Video Player component to a plane programmatically, and the video would work in editor and not in the iOS build. Tried both adding dummy shader and dummy material into the Resources folder and it wouldn't work.
So what I did was to create a dummy plane (with transform scale as 0 and position behind the camera) that has the Video Player component with a video attached. Problem solved!