Hey guys, I’m trying to make a material slowly changes it opacity. It’s not working well because I only see my material when it’s totally opaque or totally transparent(I can’t see the fade effect) . Here’s the code. Can someone tell me what I’m doing wrong?
using UnityEngine;
using System.Collections;
public class alphaControl : MonoBehaviour {
void Update () {
if(Input.GetKeyUp(KeyCode.T)) {
ToAlpha();
}
if(Input.GetKeyUp(KeyCode.F)) {
fromAlpha();
}
}
void ToAlpha () {
float alpha = transform.renderer.material.color.a;
while(alpha > 0) {
alpha -= Time.deltaTime;
print (alpha);
Color newColor = new Color(1, 1, 1, alpha);
transform.renderer.material.color = newColor;
}
}
void fromAlpha () {
float alpha = transform.renderer.material.color.a;
while(alpha < 1) {
alpha += Time.deltaTime;
print (alpha);
Color newColor = new Color(1, 1, 1, alpha);
transform.renderer.material.color = newColor;
}
}
}
Any help would be appreciated!
Thanks from now.
Guto