- Home /
Question by
Lonasora · Jul 13, 2017 at 08:44 PM ·
triggerimageimage effectsfading
My image does not get transparent all the way!
Hello!
Mm having some trouble to get my image transparent all the way, when my character goes on a trigger collider a image should pop up and the disappear when exiting the trigger but when exiting the trigger the image has an alpha of 4, but sometimes it does go down to 0 but i dont know why it somtimes does not go down all the way to 0.
Here is my code for the trigger. (fading code is in the end )
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class jump_triggers : MonoBehaviour
{
public bool onTrigger;
public Image img;
public Canvas Canvas_use;
public Transform player;
static int y; // trigger once
private void Awake()
{
Canvas_use.enabled = false;
}
// when entring the collider show the canvas
void OnTriggerEnter(Collider coll)
{
if (coll.gameObject.tag == "Player" && y == 0)
{
onTrigger = true;
Canvas_use.enabled = true;
Debug.Log("test");
StartCoroutine(FadeImage(false));
}
else
{
Canvas_use.enabled = false;
}
}
//when exiting the collider change to default
void OnTriggerExit(Collider coll)
{
if (coll.gameObject.tag == "Player" && y == 0)
{
onTrigger = false;
Canvas_use.enabled = true;
StartCoroutine(FadeImage(true));
}
else
{
Canvas_use.enabled = false;
}
y++; // trigger once
}
IEnumerator FadeImage(bool fadeAway)
{
// fade from opaque to transparent
if (fadeAway)
{
// loop over 1 second backwards
for (float i = 3; i >= 0; i -= Time.deltaTime) // fade speed (i)
{
// set color with i as alpha
img.color = new Color(1, 1, 1, i);
yield return null;
}
}
// fade from transparent to opaque
else
{
// loop over 1 second
for (float i = 0; i <= 1; i += Time.deltaTime)
{
// set color with i as alpha
img.color = new Color(1, 1, 1, i);
yield return null;
}
}
}
}
Comment
Answer by Mercbaker · Jul 13, 2017 at 08:52 PM
You should log what (3-Time.deltaTime) returns and you will get your answer as to why your image is not fading away.
Your answer
![](https://koobas.hobune.stream/wayback/20220612130215im_/https://answers.unity.com/themes/thub/images/avi.jpg)