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 SirBoboHobo · May 31, 2015 at 04:03 PM · colorlerp

Lerp Color doesn't work

Hey, I've made this script tho I'm having a little problem with it, doesn't change colors for some reason :/

     public Color[] bgColors;
     private float t = 0.05f;
     private Renderer rend;
     // Use this for initialization
     void Awake()
     {
         rend = GetComponent<Renderer>();
     }
     void Start () 
     {
         Invoke ("changeColor", 1f);
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     void changeColor()
     {
         Debug.Log ("IM HERE");
         Color.Lerp(rend.material.color, bgColors[Random.Range(0, bgColors.Length)],t);
         Invoke ("changeColor", 1f);
     }
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

2 Replies

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

Answer by DiegoSLTS · May 31, 2015 at 04:23 PM

First, If you don't change the value of "t" you'll always get the same color from Color.Lerp. Check again the documentation of Color.Lerp, the "t" value should go from 0 to 1 to get the proper transition: http://docs.unity3d.com/ScriptReference/Color.Lerp.html

Second, Lerp returns a color, it doesn't change the one you provided, so you have to use the returned color for something. Again, look at the docs.

Third, I don't understand what you're trying to do, but I guess you're also confused about the Invoke method.

If you want to lerp from one color to another and want the transition to last 1 second you have to update the color on every frame. Your "Invoke("changeColor",1f);" line calls the changeColor only once every second.

Also, you're changing the second color to a random color for the list on every call to that function, that would give really weird results.

I'll make a guess here, I think you want this code:

 public Color[] bgColors;
 private Renderer rend;
 
 // Use this for initialization
 void Awake()
 {
     rend = GetComponent<Renderer>();
 }
 
 void Start () 
 {
     StartCoroutine(changeColor());
 }
      
 IEnumerator changeColor()
 {
     Color startColor = rend.material.color;
     Color endColor = bgColors[Random.Range(0, bgColors.Length)];
     float t = 0f;
 
     while(t <= 1f) {
         rend.material.color = Color.Lerp(startColor,endColor,t);
         t += Time.deltaTime;
         yield return null;
     }
 
     StartCoroutine(changeColor());
 }
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 maccabbe · May 31, 2015 at 04:25 PM

 Color.Lerp(Color a, Color b, float t) 

returns a new Color. To change one of the passed colors you have to assign to the passed color, i.e.

 rend.material.color=Color.Lerp(rend.material.color, bgColors[Random.Range(0, bgColors.Length)],t);


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

20 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

Related Questions

lerp transparency back and forth, while cycling through rainbow? 0 Answers

How to make OnTriggerStay ColorLerp start all over again OnTrigger? 2 Answers

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

Lerp color of verts with position bigger than 1 1 Answer

Trouble with OnGUI and Update 3 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