- Home /
SetTexture Not Working In Unity 5
Unity Version (5.1.1f1)
I'm currently using the Standard Shader when creating Materials dynamically through script at runtime. When apply a texture using the method below, I'm able to see my Diffuse Texture:
SetTexture("_MainTex", myDiffTex);
However, after attempting to apply my next 2 textures, they never get updated in the viewport:
SetTexture("_MetallicGlossMap", mySpecTex);
SetTexture("_BumpMap", myBumpTex);
However, once I go over to the inspector and select the dropdown for the material, everything updates and my changes are reflected in the main view. (I've attached images demonstrating this)
Is this a bug in Unity 5?
Answer by inewland · Jul 07, 2015 at 06:15 PM
Ok so it appears the problem was with this new thing called 'EnableKeyword'.
Because of the new nature of the Standard Sharder and how variants work, you have to actually enable the keywords that you are looking to apply to this shader.
Doing this fixed it for me:
// For '_BumpMap'
material.EnableKeyword("_NORMALMAP");
// For '_MetallicGlossMap'
material.EnableKeyword("_METALLICGLOSSMAP");
If folks are looking for more information, I found these docs helpful:
http://docs.unity3d.com/$$anonymous$$anual/$$anonymous$$aterialsAccessingViaScript.html (near the bottom, tells you the keywords for the standard shader)
http://docs.unity3d.com/$$anonymous$$anual/SL-$$anonymous$$ultipleProgramVariants.html
Answer by HappyLife · Jun 16, 2017 at 04:55 AM
I don't know why,but after i invoke SetTexture twice the problem is solved and the texture of material is updated
I have same problem, unity 5.6.3.p3 have tried both keyword, and calling SetTExture twice.. adding keyword, fixed problem for android. but on iPad3 still not working.... though things work on iPhone 6s. Saw same issue with it not updating in editor unless click outside editor, then click back inside to gain foxus of editor, fixed materials.... broken on device though :(
I have a problem. $$anonymous$$y script is simple. All I'm trying to do is to change a texture of a material when a variable reaches specified value. if (blink$$anonymous$$eter > 90){ g_Health.renderer.material.SetTexture("_$$anonymous$$ainTex", h10); return; } However, Unity says that it was expecting ")", but found ",", so that means that it does not know that there should be a texture there. Could you please help?
is blink$$anonymous$$eter an int or float ? if float it should be 90f is h10 of type Texture or Texture2D ? it should be one of those. which line of code gets that error... can you split it out into several lines, ins$$anonymous$$d of one long line