2
votes

I have a trouble setting normal map via script in unity. I have a normal map and I want to assign it to a material of the target object.

renderer.material = new Material(oldMaterial);    
renderer.material.SetTexture("_BumpMap", normalTexture);

This line works for assigning the texture. But the target object's normal map is not updated until I open inspector and click material component. I can update albedo texture by using this technique and it works.
Is there a function to force material update its properties ? Any ideas?

2

2 Answers

2
votes

Try add that one:

renderer.material.shaderKeywords = new string[1]{"_NORMALMAP"};
2
votes

According to Unity's Docs, you first need to EnableKeyword : _NORMALMAP. Then, the normal map you provided can be displayed. For more explanation, you can see the following code or this website.

//Fetch the Renderer from the GameObject
renderer = GetComponent<Renderer> ();

//Make sure to enable the Keywords
renderer.material.EnableKeyword ("_NORMALMAP");

//Set the Normal map using the Texture
renderer.material.SetTexture("_BumpMap", normalTexture);