At runtime, I get the following error:
Material doesn't have a texture property '_BumpDetail'
UnityEngine.Material:GetTexture(String)
...
This is the code in question:
var a = sharedMat.HasProperty("_MainTex") ? sharedMat.GetTexture("_MainTex") as Texture2D : null;
var b = sharedMat.HasProperty("_BumpMap") ? sharedMat.GetTexture("_BumpMap") as Texture2D : null;
var c = sharedMat.HasProperty("_BumpDetail") ? sharedMat.GetTexture("_BumpDetail") as Texture2D : null;
var d = sharedMat.HasProperty("_SpecMap") ? sharedMat.GetTexture("_SpecMap") as Texture2D : null;
var e = sharedMat.HasProperty("_LightMap") ? sharedMat.GetTexture("_LightMap") as Texture2D : null;
(The error happens on the line assigning to var c)
Beyond that snippet, the code doesn’t do anything else with that material - it just calls the snippet, then immediately replaces the material (on the mesh) with a different one.
I can’t see any reason why the call to Material.HasProperty() would allow the code to execute Material.GetTexture(), if the call to GetTexture() will fail from a lack of the property in question.
The kicker is that this error only occurs when I let the code run under normal circumstances. If I put a breakpoint (seemingly anywhere?) somewhere in the method in question, every line of code will execute without error. Literally hitting the breakpoint, and then telling execution to continue, without stepping thru code at all, results in everything executing successfully. The error only occurs if I don’t pause execution at a breakpoint.
Is there some internal housekeeping or something going on that could result in a race condition internal to the Material Properties?
As a side note, I’ve tried restarting both Unity and Visual Studio, and the problem still occurs.