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 /
avatar image
0
Question by Rob 6 · Jul 11, 2011 at 01:50 PM · colorlerptransparencyalphaopacity

changes to colour alpha affect RGB values, but not in the same way

Firstly, I'll let you know that this script is currently functioning correctly - that is, the ballMaterial's alpha value fades to 0.3 while the camera is within a certain distance of the ballObject. My issue is that without the "ballColor.r", "ballColor.g" and "ballColor.b" lines in place, the RGB colour of the ballObject's material changes to (0,0,0) and stays there throughout play and even after play mode has been stopped. I simply don't understand how the RGB is getting changed to black when I only have a line modifying the alpha channel! At the very least I'd expect the RGB to fade between black and white with the alpha, but no...

If I'm doing something slightly wrong, it would be handy to know - and if this is just the way it works, is there a more elegant way of overriding the RGB? The multi-line solution I have at the moment seems really clumsy!

 void FixedUpdate()
 {
     if (Vector3.Distance(ballCamera.position, ballObject.position) < fadeDistance)
     {
         FadeBall(0.3f);
     }
     else
     {
         FadeBall(1.0f);
     }
 }
 
 void FadeBall(float targetOpacity)
 {
     ballColor.a = Mathf.Lerp(ballColor.a, targetOpacity, /*Time.time **/ fadeDamping);
     ballColor.r = 1;
     ballColor.g = 1;
     ballColor.b = 1;
     ballMaterial.color = ballColor;
 }
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

1 Reply

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

Answer by Jessy · Jul 11, 2011 at 04:52 PM

You haven't posted where ballColor comes from, but your problem is that it's a newly-created Color every time FadeBall is run. floating point variables are zero upon creation, and Color is a structs that contains four of them. Setting ballColor to be ballMaterial.color at the beignning of the funtion will solve the problem, but if you're bothering to store the variable, you probably should fix your code so that you're not creating a new Color for it all the time. I recommend keeping a class with extension methods around, and having a method like SetAlpha in there:

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

  void FadeBall(float targetOpacity) {
         ballMaterial.SetAlpha(Mathf.Lerp(ballColor.a, targetOpacity, whatever));
     ballColor = ballMaterial.color;
 }
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 Rob 6 · Jul 11, 2011 at 07:28 PM 1
Share

Spot on regarding the colour issue!

I'm not too comfortable with C# yet, still learning some basic concepts elsewhere at the same time as winging it in Unity - do you mean keeping extension methods in a class in the same script, or in another one in the project? And if my guesstimation is correct, would that behave sort of like a macro?

See, this is the kind of stuff I need to be learning, and it's unfortunately the kind of stuff that the Unity documentation doesn't go into!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Change alpha value to transparent when colliding 1 Answer

How to change the alpha of a spawned particle? 1 Answer

Cannot set UnityUI.Image opacity when place on a UnityUI.Button 2 Answers

Transperency not working correctly on imported objects. 1 Answer

Mesh transparency halfway cutoff script 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