Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 /
avatar image
0
Question by weltraumaffe · Jul 24, 2015 at 01:39 PM · c#unity 5shader

Set alpha in Standard Shader

I want to set the alpha of a material with the Standard Shader. I created the following method.

 public void SetAlpha(float alpha) {
     Material mat = new Material(_meshRenderer.sharedMaterial);
     mat.color = new Color(mat.color.r, mat.color.g, mat.color.b, alpha);
     _meshRenderer.sharedMaterial = mat;
 }

However after using the method the new alpha value is not displayed. When I inspect the Material the new alpha value is shown. Only if I modify the alpha value by hand does the value get updated.

EDIT I already made sure that the render mode is set to transparent in the base material.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by peaveyyyy · Jul 24, 2015 at 02:48 PM

  void SetAlpha(float value)
     {
         Color color = yourgameobject.GetComponent<Renderer>().materials[0].color;
         color.a = value;
         yourgameobject.GetComponent<Renderer>().materials[0].color = color;
     }
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
avatar image
0

Answer by Veldars · Jul 24, 2015 at 01:58 PM

Hi to change the alpha most of time its the alpha of the main texture that you need to change...

 Material mat = new Material(_meshRenderer.sharedMaterial);
 Color col = mat.mainTexture.color;
 col.a = alpha;
 mat.mainTexture.color = col;

Hope this help..

Comment
Add comment · Show 6 · 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
avatar image weltraumaffe · Jul 24, 2015 at 02:00 PM 0
Share

mat.mainTexture.color does not exist

avatar image Veldars · Jul 24, 2015 at 02:10 PM 1
Share

arf sorry, I will check in my script how I manage things like that. Here i mix up with sprite...

avatar image Veldars · Jul 24, 2015 at 02:12 PM 1
Share

If you change the color that works ? Or di you try to change only the color directly :

     Color color = _meshRenderer.shared$$anonymous$$aterial.color;
     color.a = alpha;
     _meshRenderer.shared$$anonymous$$aterial.color = color;

Sorry I'm a bit lost without my computer to try by myself...

avatar image weltraumaffe · Jul 24, 2015 at 02:13 PM 0
Share

Yep that works. The strange thing is the inspector shows the correct new alpha value. But only after editing by hand in the inspector it get's updated...

avatar image Veldars · Jul 24, 2015 at 02:19 PM 0
Share

Yes this is really srtrange, what is your Unity version ? Did you try to simply close and reopen Unity ? (Sometime unity do misterious things ^^))

Show more comments

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

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to blink a color in unity (legacy shaders/difuse) which is the main color., 0 Answers

How to detect if the Device can run the shader ? 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to detect if an object is within another object??? (Hide and seek type game). Thanks! 2 Answers


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