I’m trying to make a bunch of objects fade and move to create a UI that moves into place.
when I start my code with the alpha value at 0 and run the code the alpha value returns to 0 but the object is still visible but instead of being white its very transparent but black but if I start the code with alpha set to 1 it becomes completely transparent even when running it multiple times consecutively. I don’t understand why its doing this or how due to the fact that the alpha is 0 at the end and it works when repeating when started with alpha set to 1
I want the object to become opaque and move down at the same time and separately at another point in time move up and become transparent.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class ButtonPress : MonoBehaviour
{
public string targetTag;
public UnityEvent<GameObject> OnEnterEvent;
GameObject uimodel;
MeshRenderer renderer;
Material material;
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == targetTag)
{
uimodel = GameObject.Find("tester model");
renderer = uimodel.GetComponent<MeshRenderer>();
material = renderer.material;
//StartCoroutine(FadeOut());
StartCoroutine(FadeIn());
//OnEnterEvent.Invoke(other.gameObject);
}
}
IEnumerator FadeIn()
{
for (float alpha = material.GetFloat("_mask_alpha"); alpha < 1.1f; alpha += 0.1f)
{
material.SetFloat("_mask_alpha", alpha);
yield return new WaitForSeconds(0.001f);
Vector3 pos = uimodel.transform.position;
pos.y = (float)(pos.y - 0.01);
uimodel.transform.position = pos;
}
yield return new WaitForSeconds(2f);
StartCoroutine(FadeOut());
}
IEnumerator FadeOut()
{
for (float alpha = material.GetFloat("_mask_alpha"); alpha > -0.1f; alpha -= 0.1f)
{
material.SetFloat("_mask_alpha", alpha);
yield return new WaitForSeconds(0.001f);
Vector3 pos = uimodel.transform.position;
pos.y = (float)(pos.y + 0.01);
uimodel.transform.position = pos;
}
}
}
user26739890 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.