Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by lucasabritta · Jul 23, 2016 at 06:38 AM · materialrenderer

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");
         }
  }
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Owen-Reynolds · Jul 23, 2016 at 03:07 PM 0
Share

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?

avatar image lucasabritta Owen-Reynolds · Jul 25, 2016 at 12:10 PM 0
Share

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.

avatar image NoseKills lucasabritta · Jul 25, 2016 at 03:55 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Render Pipeline Material Issue 1 Answer

Assigning a sharedMaterial to multiple renderers that change together 0 Answers

material color not changing 1 Answer

Is there a way to get what material was hit with RaycastHit.textureCoord? 0 Answers

Gameobject Renderer not working. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges