- Home /
Fading an object with Color.Lerp
The following script isn't working. I copied it directly from the docs, and it's supposed to fade lerpedColor
to black; however, it fades it instantaneously. Why?
using UnityEngine; using System.Collections;
public class example : MonoBehaviour { public Color lerpedColor = Color.white; void Update() { lerpedColor = Color.Lerp(Color.white, Color.black, Time.time); } }
I'm not even applying the color yet, just watching it in the Inspector while my game is running. What is going on here?
Use this for fading objects: http://www.unifycommunity.com/wiki/index.php?title=Fade
Answer by Jessy · Feb 01, 2011 at 03:24 PM
The only thing that would make that happen is that you trigger the code after 1 second of game time has elapsed. (Time.time is in seconds and Lerp operates between 0 and 1.)
Just to avoid deductive error, I copied it and pasted it into a new C# script. It did what the docs would have you believe it would do. Make sure that you lerpedColor shows up as white in the Inspector first, though. That code is initiating the color value outside of a function, which means it will fire unpredictably; it's not good practice, but nobody actually wrote that. It was just run through some sort of automatic JS->C# converter.
Well, I had originally had an if statement in there so I could set a variable in the Inspector to trigger the lerp, but I stripped it out for the sake of simplicity. Same problem with or without if statement.
Really? Wow. $$anonymous$$y computer's being funky these days. Yeah, it does start as white, but then when I trigger it, it just jumps to black.
Yes, it appears enclosing it in an if statement caused the problem. Can you think of any workarounds? I need to be able to fade objects. Thanks for your time, btw. I appreciate it.
If you've changed your code and are having problems, maybe you could edit your post to include the new code. (Also, the use of Time.time in the Lerp() example in the documentation is misleading at best. At any rate, it's certainly led many new Unity users astray...)
Yes, I found that to be the problem, which I've solved now. Thanks, Jessy and Jesse!
Your answer
Follow this Question
Related Questions
Lerp Color doesn't work 2 Answers
How can I change the color of a light over time? 3 Answers
How do I loop a Color32.Lerp? 3 Answers
How to make two colors lerp independently in this code? 1 Answer