Changing a normal map or general texture parameters from code won't update.

Hello!

I’m placing the normal map of the standard material from code, it’s basically a randomized normal map from an array.
Now i have to mention that this doesn’t happen with normal maps only, i’ve had this problem with emissive color too.
When i set them they just simply don’t update, but I know for a fact that they DO work, here is why;
When i click the gameobject in the editor, navigate to the material and click it it actually updates to whatever it should be.
Is there a way to fix this?

Here is the code I’m using to set the normal map:
currObj.GetComponent().material.SetTexture(“_BumpMap”, paperNormals[Random.Range(0, paperNormals.Length)]);

I’m also using .material.mainTexture which updates properly.

Ok… I don’t get this issue whatsoever myself… Maybe the material instancing is stuffing around? Try using:
Shader.SetGlobalTexture("_BumpMap", paperNormals[Random.Range(0, paperNormals.Length)]);

this sets ALL materials using the “BumpMap” shader parameter to that texture. If that doesn’t work… try using ONE texture only and see how that goes.

I also got this problem…

When I make a new GameObject at runtime, and add meshfilter and meshrenderer and main texture and so on… and then add a normalMap with:

meshRender.material.SetTexture(“_BumpMap”, tex);

then It won’t show in the editor GameView before I press the material/shader in the inspector on the gameobject in the hierachy…

I can fix it with a prefab that already has some random normal map added from the beginning, and then overwrite it runtime, that will work… but it is not preferred…

is it a bug in Unity 5.1.1f1 ?

1 Like

Maybe the bumpmap property has a glitch? worth a bug report I think. The “_BumpMap” property is something the editor recognises as a normal map, so I would be gathering this issue is a conflict between a couple of things internal to Unity.

Other than the above, I find no other explanation… :smile:

Unity will use the most basic variant of the shader based on what is used when the game is build / run
You have to use the Material.EnableKeyword function to re-enable the unused features (like normal map in your case).
For normal map the keyword is “_NORMALMAP” .
You can find a explanation and list of all the keywords for the standard shader on this documentation page.

1 Like

Thank you!
I had to use both:

newMat.EnableKeyword(“_NORMALMAP”);
newMat.EnableKeyword(“_DETAIL_MULX2”);

before it worked here :slight_smile:

@Ayes : Make sure to read the second paragraph on the documentation page or you will get wrong behavior in your builds.

1 Like

Was that all it was?! :hushed: That’s a bit convoluted… at least for beginners. Unity should probably find a better workaround for this one.

Have read it now, so basically, its always best to use a prefab with a material on, where the textures you want to use is filled out… so the shader will be correctly combined to runtime…