Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Trorlroprlro · Nov 27, 2013 at 04:59 PM · colorrenderer

renderer.material.color.a

I'm having a problem with this:

   void Update(){

     var staticObject = GameObject.Find("staticObject");
 
     if(staticObject)
         {
 
             staticRenderer = staticObject.renderer;
 
             staticRenderer.material.color.a = 0.0f;
         }
         else
         {
             Debug.LogWarning("Not found");
         }
     }

It gives me:

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

How can i fix this?

Comment
Add comment · Show 1
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 markmmiller · Nov 27, 2013 at 05:46 PM 0
Share

I'm not sure where the problem is, I just ran it and it works. Is that definitely the code that's causing the problem?

3 Replies

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

Answer by Subhajit-Nath · Nov 27, 2013 at 05:30 PM

In C# you need to write it like this:

staticRenderer.material.color = new Color(1.0f, 1.0f, 1.0f, 0.0f);

Comment
Add comment · Show 2 · 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 Spinnernicholas · Nov 30, 2013 at 01:46 AM 0
Share

This will set everything, not just the alpha.

avatar image ABerlemont · Jun 11, 2014 at 10:59 AM 0
Share

The other answers are better to just switch the alpha

avatar image
6

Answer by Spinnernicholas · Nov 27, 2013 at 06:05 PM

staticRenderer.material.color is a property, that means it is basically a function and it returns a Color struct. Because Color is a struct, and not a class, you get a copy. Changing the copy does absolutely nothing to the original.

If it were a class, you would get a reference and you could modify it like that you are trying.

The common form for what you are trying to do is:

  1. Retrieve the original.

  2. Modify it.

  3. Store it back to where it was.

Here it is in code:

 Color color = staticRenderer.material.color;
 color.a = 0.0f;
 staticRenderer.material.color = color;

It is sometimes a good idea to create helper functions to make it easier.

 function changeAlpha(var color, var newAlpha)
 {
   color.a = 0.0f;
   return color;
 }

Then you could use it like this:

 staticRenderer.material.color = changeAlpha(staticRenderer.material.color, 0.0f);
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 NicoLocke · Jun 11, 2014 at 10:31 AM 0
Share

Helped a lot, thanks bud!

avatar image TheXyon · Aug 19, 2014 at 02:07 AM 0
Share

will this same solution work with "renderer.material.color.r" ?

avatar image echolink · Aug 10, 2018 at 03:18 PM 0
Share

very good explanation. even newb like me could understand thanks

avatar image
0

Answer by Key_Less · Nov 27, 2013 at 06:02 PM

The alpha channel in the color component is readonly and cannot be assigned. So if you only want to change the alpha value, you'll need to store the color in a temp variable as the error suggests, then change the temp and reassign the color value.

 var color = staticRenderer.material.color;
 color.a = 0.0f;
 staticRenderer .material.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

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

24 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 avatar image

Related Questions

Changing two different objects renderer colour 1 Answer

How to redner a single texture, multiple colors without increasing draw calls. 1 Answer

Fade out a line renderer? 1 Answer

Fade out a material that has a texture 1 Answer

Dynamically fill color in a gameobject? 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