- Home /
How do I make a cube fade to a colour?
I want to do something seemingly very simple: I have a cube and I wanted it just a plain colour. Instead of importing any texture/sprite I just decided to make a material with a shader of Mobile/Unlit (any better one to choose if I want it to just be lit all the time and ignore the lighting?) and then set the colour to white. I applied this to material element 0 of my cube's mesh renderer.
Then I wrote a script and attached it to the cube. There is a public var that holds a Color
type and I put this in the Update()
method:
renderer.material.color = this.colourToChangeTo
Nothing happens however. My end-goal is to fade from one colour to another, but first I need to be able to actually set the colours.
When you change the color in the editor, with the color picker of the material, does it change anything ? $$anonymous$$ight be the shader.
Yeah it changes no problem there doing it manually
There is actually a sample code that does this in the Unity Script reference.
http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$aterial-color.html
This will alternate back and forth from one color to another, because it uses math.pingpong. You can get rid of that part. This sample should get you on track.
Answer by DiGiaCom-Tech · Mar 20, 2014 at 02:16 PM
Take a look at this tutorial:
http://unity3d.com/learn/tutorials/modules/beginner/scripting/lerp
This should give you what you need to do what you want (it's towards the end of the video). I recently found that you can LERP between materials as well.