I’ve dragged Image in gameobject “uiElement” and the Image should ‘Fade In’ when gameobject “Plane” has Y-position = 40. But the code is not working. Please help!!
using System.Collections;
using UnityEngine;
public class AO : MonoBehaviour
{
public GameObject uiElement;
public GameObject Plane;
SpriteRenderer rend;
void Start()
{
rend = uiElement.GetComponent<SpriteRenderer>();
Color c = rend.material.color;
c.a = 0f;
rend.material.color = c;
}
void Update()
{
if (this.transform.position.y >= 40)
{
StartCoroutine("FadeIn");
}
}
public IEnumerator FadeIn()
{
for (float f = 0.05f; f <= 1; f += 0.05f)
{
Color c = rend.material.color;
c.a = f;
rend.material.color = c;
yield return new WaitForSeconds(0.05f);
}
}
},using System.Collections;
using UnityEngine;
public class AO : MonoBehaviour
{
public GameObject uiElement;
public GameObject Plane;
SpriteRenderer rend;
void Start()
{
rend = uiElement.GetComponent<SpriteRenderer>();
Color c = rend.material.color;
c.a = 0f;
rend.material.color = c;
}
void Update()
{
if (this.transform.position.y >= 40)
{
StartCoroutine("FadeIn");
}
}
public IEnumerator FadeIn()
{
Debug.Log("Entered coroutine");
for (float f = 0.05f; f <= 1; f += 0.05f)
{
Color c = rend.material.color;
c.a = f;
rend.material.color = c;
yield return new WaitForSeconds(0.05f);
}
}
}