- Home /
Question by
Eagle-Bound · Sep 07, 2016 at 09:27 PM ·
c#colornoobopacity
color won't change
In my game, the opacity of a color is supposed to increase, wait for a second, and go back. However, the color opacity goes up, but not down. If anyone knows why, please tell me. My script:
using UnityEngine; using System.Collections;
public class GameManager : MonoBehaviour {
public SpriteRenderer[] colors;
private int colorSelect;
public float lit;
private float litCounter;
// Use this for initialization
void Start () {
if (litCounter > 0) {
litCounter -= Time.deltaTime;
} else {
colors [colorSelect].color = new Color (colors [colorSelect].color.r, colors [colorSelect].color.g, colors [colorSelect].color.b, 0.5f);
}
}
// Update is called once per frame
void Update () {
}
public void StartGame () {
colorSelect = Random.Range (0, colors.Length);
colors [colorSelect].color = new Color (colors [colorSelect].color.r, colors [colorSelect].color.g, colors [colorSelect].color.b, 1f);
litCounter = lit;
}
}
Thanks!
Comment
Best Answer
Answer by doublemax · Sep 07, 2016 at 09:40 PM
Start() is only called once. You probably wanted that code there in Update().
Answer by JEFFDOG11111 · Sep 07, 2016 at 09:44 PM
you can use IEnumerator..
eg: all you do is call : StartCoroutine( OpacityAnimate () );
IEnumerator OpacityAnimate ()
{
// Scipt to Make Color oppacity go Up using "While loops"
while ( opacity < 1f )
{
opacity + 0.001f;
}
yield return new WaitForSeconds (1); // this will pause the opacity for 1 sec
// Scipt to Make Color oppacity go Down using "While loops"
while ( opacity > 0.1f )
{
opacity - 0.001f;
}
}