Search Unity

GetTexture() Error

Discussion in 'Scripting' started by slippyd, Apr 4, 2007.

  1. slippyd

    slippyd

    Joined:
    Jun 11, 2005
    Posts:
    129
    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:
    Code (csharp):
    1. function Start ()
    2. {
    3. var tex = renderer.material.GetTexture("_BumpMap");
    4. if (tex)
    5. print ("My bumpmap is " + tex.name);
    6. else
    7. print ("I have no bumpmap!");
    8. }
    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.
     
  2. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    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.
     
  3. keeperkai2

    keeperkai2

    Joined:
    Oct 26, 2012
    Posts:
    14
    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?)
     
    ClearRoseOfWar likes this.