GetTexture() Error

Is there a way to find out if a material has a texture (or other property) by name without getting a runtime error?

I am trying to get a “_BumpMap” texture if one exists and avoid an error if one doesn’t exist. However, calling GetTexture(“_BumpMap”) gives me a runtime error if it doesn’t exist.

It’s interesting that in the documentation page for Material.GetTexture (http://unity3d.com/Documentation/ScriptReference/Material.GetTexture.html), they give the following sample code:

function Start ()
{
var tex = renderer.material.GetTexture("_BumpMap");
if (tex)
print ("My bumpmap is " + tex.name);
else
print ("I have no bumpmap!");
}

Which does work and prints “I have no bumpmap!”, but also gives a “Material doesn’t have a texture property ‘_BumpMap’ UnityEngine.Material:GetTexture(String)” on the GetTexture() line.

From the nature of the problem, I could assume that the Shader class would have a isTextureSupported() function or something like that.

Note that this is only a problem when the Shader doesn’t have the “_BumpMap” property, not when the shader has the capability, but the bumpmap is none in the editor.

Yes, we’re adding a HasProperty() function in a future release. For now you could just ignore the error - it is printed to the console, but it’s not an exception; i.e. the script continues to execute.

So if my component throws this error, will I still be able to put this on unity store? Because I recall reading that the components we provide should not throw any exceptions under any condition.(so errors like these are ok right?)

1 Like