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 /
  • Help Room /
avatar image
0
Question by RaHaN_GS · Apr 11, 2016 at 03:09 PM · materialscolor.lerp

Lerping a material _TintColor: why does it work in a case and not the other?

Hi everyone.

I'm building some simple FX effects by changing material properties on some FX meshes.

I have the follwing code that works perfectly:

     public class FX_buttons : MonoBehaviour { 
     public Color colorFull;
     public Color colorEmpty;
     public float fadeTime = 1f;
     public float smoothness = 0.02f;
     float t = 0;
 
     void Update () {
         gameObject.GetComponent<Renderer> ().material.SetColor ("_TintColor", Fade(colorEmpty, colorFull, smoothness, fadeTime));
         }
 
     /// <summary>
     /// Fade from startColor to endColor in duration seconds.
     /// </summary>
     /// <param name="startColor">Start color.</param>
     /// <param name="endColor">End color.</param>
     /// <param name="duration">Duration in seconds.</param>
     Color Fade (Color startColor, Color endColor, float smoothness, float duration)
     {
         Color c = startColor;
         float increment = smoothness/duration;
         while (t <1)
         {
             c = Color.Lerp(startColor, endColor, t);
             t += increment;
             return(c);
         }
 
         return (endColor);
     }

However, I tried another Fade definition as follows:

 void Fade (Material material, Color startColor, Color endColor, float smoothness, float duration)
     {
         Color c;
         float increment = smoothness/duration;
         while (t <1)
         {
             c = Color.Lerp(startColor, endColor, t);
             t += increment;
             material.SetColor("_TintColor", c);
         }
     }

and using this Method in the Update()...

  void Update () {
         Fade (gameObject.GetComponent<Renderer>().material, colorEmpty, colorFull, smoothness, fadeTime);
     }

And it doesn't actually lerp. The Material's _TintColor simply passes from one to the other instantaneously... and I can't quite figure out why... any ideas?

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
0

Answer by NoseKills · Apr 11, 2016 at 06:41 PM

  while (t <1)  {
        c = Color.Lerp(startColor, endColor, t);
        t += increment;
        material.SetColor("_TintColor", c);
  }

Unity runs all user code in a single thread, which means that at any given time only one line of code is being run. So when you write a loop like this, the execution runs only the code in the loop until t goes over 1 and subsequently the color fades all the way to the end.

"Only the code in the loop" meaning that not a single other MonoBehaviour can run their Update at the same time, the screen cannot be repainted etc. etc.

In your working code you have a return statement inside the while-loop so it will only ever "loop" once. It's effectively an if-statement though you used "while".

Since you call the Fade() function inside Update(), you should basically use "if" in both versions of the function. What you see on the screen only gets repainted between Update() calls so it almost never makes sense to change color or do any other visual modifications in a loop: the objects state only gets drawn on the screen after exiting the loop (which you have experienced at first hand here)

Have you tried what happens when you put an endless loop into your code ?

 while (true);

Whole Unity freezes. It gets stuck executing that one line in your code and it can't repaint or gather input from buttons so there's no way to make it stop. All you can do is close it from task manager.

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 RaHaN_GS · Apr 11, 2016 at 07:44 PM 0
Share

Thanks a lot for this clear explanation.

Now, I understand what happens and why ;p

I guess it would work, then if I call the function recursively with one "increment" each call until it reaches the desired state (like t=1)?

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

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

Related Questions

Instantiating a renderer and its materials as a backup to swap due to highlighting 0 Answers

How do I change the color of a material in mesh render? 0 Answers

Bitmap applied as material to a cylinder 1 Answer

Change material in runtime 2 Answers

Get material of Children's children and Change tag help 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