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
1
Question by looMeenin · Jan 28, 2015 at 10:46 PM · uiunity4.6

How to change alpha channel of ui image?

Why can't I change the alpha channel of my button image using this line of code:

 button.image.color.a = 0.5f;

I am getting this error:

error CS1612: Cannot modify a value type return value of `UnityEngine.UI.Graphic.color'. Consider storing the value in a temporary variable

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

3 Replies

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

Answer by Superrodan · Jan 28, 2015 at 11:15 PM

You can't change an individual color value directly I can't explain why but I can tell you how to fix it.

 Color temp = button.image.color;
 temp.a=0.5f;
 button.image.color = temp;
Comment
Add comment · Show 1 · 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 Superrodan · Jan 28, 2015 at 11:18 PM 0
Share

You have to do this with all types of data that are made up of smaller types of data.

So, for example, any variable that uses a Vector3, like position or rotation would be handled the same way. First you store the variable in a temporary holder and then modify one part of the holder. Then you set your object to be equal to that holder.

If you wanted to move your object to the right, for example, you would have to do this:

 Vector3 tempVector = myObject.transform.localPosition;
 tempVector.x+=100;
 myObject.transform.localPosition = tempVector;

You'll use this method a LOT when program$$anonymous$$g in Unity, I've found.

avatar image
0

Answer by Michio-Magic · May 30, 2015 at 05:20 PM

This worked for me:

 var white : UI.Image;  //select Image in Canvas
 private var fade : float;
 private var endlevel : boolean;
 
 function FixedUpdate() {
 
   if (Input.GetKeyDown("f"))endlevel=true; //used to test it
 
   if(endlevel){
     fade+=0.01f;  if(fade>1)fade=1;  // from 0.0 to 1.0
     white.color.a=fade;
   }
 }

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 MrProcessor · Jun 09, 2020 at 10:03 AM

I made it like this: public Image myImageObject; public Color opaque; public Color transparent; void MakeOpaqe() { myImageObject.GetComponent().color = opaque; } void MakeTransparent() { myImageObject.GetComponent().color = transparent; }

In the editor, I made the opaque and transparent colors as I desired. This was easier for me I hope it helps <3

Comment
Add comment · Show 5 · 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 erenalkan2426 · May 19, 2021 at 09:18 AM 0
Share

@MrProcessor It gives me an error like this:

alt text

resim-2021-05-19-121728.png (13.9 kB)
avatar image Hellium erenalkan2426 · May 19, 2021 at 09:24 AM 0
Share

Make sure you are referencing the Image component from the UnityEngine.UI namespace, not an image from another namespace or a custom Image class.

To avoid any confusion:

 private UnityEngine.UI.Image image;
 
 void Update()
 {
      image = GetComponent<UnityEngine.UI.Image>();
      // ....    
 }

avatar image erenalkan2426 Hellium · May 19, 2021 at 09:34 AM 0
Share

@Hellium thank you. the code does not fail but image's opacity value does not decrease. My code:

alt text

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

Unity 4.6: Position jumps when switching Canvas render mode from Overlay to camera in code 0 Answers

Unity 4.6 UI onClick listener doesn't work 4 Answers

Why is 'material.color = Color.grey' turning all my sprites in my game grey? 1 Answer

Help appreciated with new UI system (4.6 beta) 1 Answer

Play video using native code in Unity3d via NDK 0 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