Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
5
Question by Benny 1 · Apr 24, 2011 at 09:25 PM · renderertransparencyfadecs1612

Fading out using render.material.color.a +=/-= doesn't work

Hi,

I'm trying to fade out my object by reducing its alpha but it seems reluctent to accept this code;

renderer.material.color.a -= 0.1f;

The error message I get says the following;

"Error: Cannot modify the return value of 'UnityEngine.Material.color' because it is not a variable"

I do not see how this is not a variable I can modify. I vaguely remember using this code before though now I seem to be missing something.

EDIT IN RESPONSE TO ANSWER;

using UnityEngine; using System.Collections;

public class MyRenderer : MonoBehaviour {

 // Use this for initialization
 void Start () {

 }

 // Update is called once per frame
 void Update () {

     if (Input.GetMouseButtonUp(0))
     {
         Color color = renderer.material.color;
         color.a -= 0.1f;
         renderer.material.color = color;
     }
 }

}

This doesn't do anything when I attach it to a gameobject. Created a new gameobject, added the script, ran it, did nothing :( I'm using the latest version of unity btw. Thing is I didn't have this problem when using 2.6

Comment
Add comment · Show 11
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 Justin Warner · Apr 24, 2011 at 09:41 PM 0
Share

It's working for me... $$anonymous$$aybe post some more script you're using?

avatar image Jessy · Apr 24, 2011 at 09:56 PM 1
Share

It works in JS but not C#.

avatar image Jessy · Apr 24, 2011 at 10:19 PM 1
Share

AngryOld$$anonymous$$an, that's irrelevant, all that matters is that the shader you're using has a property called _Color, and that the alpha channel of is mapped to some effect.

avatar image Bunny83 · Apr 24, 2011 at 10:29 PM 2
Share

The problem is that Color is a value type. $$anonymous$$aterial.color is not a variable but a property (with get and set method). By querying .color the get method returns a copy of the Color struct. The += operator just works on the struct member .a of the temporarily returned copy. The value get changed, but the change happens on the copy. To change the actual property you have to assign a Color struct to .color in order to invoke the setter. As Jessy said it will work in JS but in C# it will only work if you do it with a temp-variable. $$anonymous$$ost C# examples do it the right way.

avatar image Bunny83 · Apr 24, 2011 at 10:59 PM 1
Share

And what effect do you expect from a opaque diffuse shader when you tweak the inco$$anonymous$$g alpha value? In order to have a transparent effect you need to use a transparent shader that uses the alpha value to perform alpha blending.

Show more comments

5 Replies

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

Answer by Jessy · Apr 24, 2011 at 09:56 PM

Are you using an old version of Unity? The current version should tell you

Cannot modify a value type return value of `UnityEngine.Material.color'. Consider storing the value in a temporary variable

You just do what it says:

Color color = renderer.material.color;
color.a -= 0.1f;
renderer.material.color = color;

Adjusting just the alpha of the main color is a pretty common operation, so you may want to use this extension method until a better option is available. An extension property would be better, but those aren't in C# yet.

using UnityEngine;

public static class ExtensionMethods {

public static void SetAlpha (this Material material, float value) { Color color = material.color; color.a = value; material.color = color; }

}

.

renderer.material.SetAlpha(renderer.material.color.a - .1F);
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 Benny 1 · Apr 24, 2011 at 10:32 PM 0
Share

not working for me, I can see the value change in the inspector but nothing appears to be happening on screen, check the code i'm using, I edited my question

avatar image Jessy · Apr 24, 2011 at 10:35 PM 1
Share

If there aren't any errors, then you're just not using a shader that responds to changes in _Color's alpha value. Are you seeing the little white bar change in the material panel? Your code works fine for me.

avatar image Benny 1 · Apr 24, 2011 at 10:37 PM 0
Share

Yes that was the change I was referring to

avatar image Benny 1 · Apr 24, 2011 at 11:08 PM 0
Share

It's working now thank you :)

avatar image Zenov · May 17, 2015 at 06:21 AM 0
Share

just wanted to post an update: renderer is now deprecated http://docs.unity3d.com/ScriptReference/GameObject-renderer.html

Show more comments
avatar image
12

Answer by joe_bar · May 30, 2012 at 09:45 PM

This post is pretty old, but for those of you who might come across this problem. Make sure that your material shader is set to Transparent/Diffuse or some form of transparent material. Otherwise changing the alpha component does nothing.

An alternative to decrementing the alpha component :

 renderer.material.color -= new Color(0,0,0,.10f);

 

Though not super efficient its gets the job done. ,

Comment
Add comment · Show 4 · 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 bustedkrutch · Aug 28, 2013 at 03:46 AM 0
Share

Thanks a bunch for this tip. I thought it was something like this but really didn't have enough experience to "know" what the real problem was.

avatar image Simon 9 · Sep 29, 2013 at 05:53 PM 0
Share

Great! This is exactly what I was missing.

avatar image awplays49 · May 17, 2015 at 09:48 PM 0
Share

What material can be completely opaque and completely transparent?

avatar image ajayajayaj awplays49 · Sep 10, 2016 at 06:41 PM 0
Share

You just need to set the rendering mode to transparent

avatar image
4

Answer by jmasinteATI · Aug 13, 2015 at 08:10 PM

In Unity 5:

 Color color = GetComponent<Renderer>().material.color;
 color.a -= 0.1f;
 
 GetComponent<Renderer>().material.SetColor("_Color", color);

If you're using Standard Shader, make sure the Rendering Mode is set to transparent.

http://docs.unity3d.com/ScriptReference/Material.SetColor.html

Comment
Add comment · Show 3 · 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 joshvamp · Dec 05, 2015 at 06:57 PM 0
Share

how can i set rendering mode ? I'm beginer and can work with sprites? please

avatar image jmasinteATI joshvamp · Dec 07, 2015 at 04:33 PM 0
Share

In the material component in the Inspector, beneath the name of the shader you're using, you'll see "Rendering mode" with a drop down box that displays the Rendering $$anonymous$$ode. By default, it's set to "Opaque." This is for the standard shader. Not all shaders have this drop down box.

Here is a screenshot: http://docs.unity3d.com/$$anonymous$$anual/StandardShader$$anonymous$$aterialParameterRendering$$anonymous$$ode.html

avatar image shenjiexiang1994 · May 29, 2018 at 10:07 AM 0
Share

That works! Thanks for this post

avatar image
-2

Answer by Kieran Chandler · Jun 17, 2012 at 07:43 PM

try this

 color.a = new Color(color.a - 0.1, color.r, color.g, color.b);
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 Foysal-Ahmed-Emon · Jun 21, 2016 at 04:55 AM

 public static IEnumerator FadeOutThreeD (this Transform t, float targetAlpha, bool isVanish, float duration)
     {
         Renderer sr = t.GetComponent<Renderer> ();
         float diffAlpha = (targetAlpha - sr.material.color.a);
 
         float counter = 0;
         while (counter < duration) {
             float alphaAmount = sr.material.color.a + (Time.deltaTime * diffAlpha) / duration;
             sr.material.color = new Color (sr.material.color.r, sr.material.color.g, sr.material.color.b, alphaAmount);
 
             counter += Time.deltaTime;
             yield return null;
         }
         sr.material.color = new Color (sr.material.color.r, sr.material.color.g, sr.material.color.b, targetAlpha);
         if (isVanish) {
             sr.transform.gameObject.SetActive (false);
         }
     }






 using UnityEngine;
 using System.Collections;
 
 public class VanishObject : MonoBehaviour
 {
 
     void Start ()
     {
         
         StartCoroutine (transform.FadeOutThreeD (0, true, 10));
     }
 
 }
 
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

11 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

Related Questions

Interesting Idea, Although not sure if possible. 1 Answer

How to fade out an object with no renderer (and how is that even possible?!) -1 Answers

transparency and color change in individual GO 1 Answer

How to Make Part of Object Between Camera and Player Transparent 2 Answers

Changing two different objects renderer colour 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