How to know if renderer.material.GetFloat() exist
I have code like that:
Renderer render = gameObject.GetComponent<Renderer>();
Material material = render.material;
if (material.HasProperty("_MainTexOpacity"))
{
float var = material.GetFloat("_MainTexOpacity");
}
It Works in some materials but when i have some material that have the property "_MainTexOpacity" but dont have any Float, appears this error:
Material doesn't have a float or range property '_MainTexOpacity' UnityEngine.Material:GetFloat(String)
My question is, have some way to see if the material have float? I wanna do somethink like that:
Renderer render = gameObject.GetComponent<Renderer>();
Material material = render.material;
if (material.HasProperty("_MainTexOpacity"))
{
if (material have GetFloat)
{
float var = material.GetFloat("_MainTexOpacity");
}
}
The normal way is not to make materials like that, or to just know ahead of time. Do you have some where $$anonymous$$ainTexOpacity is an int? If so, why? Change that name. Or you can tag everything using $$anonymous$$TO in some funny way, and check the tag, etc... .
But, I'd track this down to be sure. $$anonymous$$aybe you're really getting the error in a place where you don't check for existence, and the material really doesn't have _$$anonymous$$TO?
When this error appears i checked if he exist, my quest is if have some way to see after i see if the material exist if this material have float or not before i use GetFloat.
If you check if (material.HasProperty("_$$anonymous$$ainTexOpacity"))
, it returns true but you get the error, that means there's a property of some other type than float with that name.
I don't think there's a method for checking the type of a property with name. So if you can, you should fix the fact that there are properties with the same name but different type in the shaders... Just like @Owen-Reynolds said...
If all the shaders certainly have a float by that name and you still get that error, you should create a simplified project to reproduce the bug and report it.
Answer by lucasabritta · Jul 28, 2016 at 12:28 PM
I Find the problem and the solution as well.
I have a Object that have 2 materials in it and i try to acess each one separated just by using .HasProperty(), when i do this, somehow, sometimes the material that doesnt have the "_MainTexOpacity" has property was caught so i put in my condition the .shader.name.CompareTo(), to compare the shader name too, and then i only caught the material that i want.
Thanks to @Owen-Reynolds and @NoseKills for the Help.