hey, i need help. When an object1 (UI image) touches another object (UI image), object1 disappear and pop up image shows up for few second and disappear again. Any help? code, tutorial? Thanks. I have got this so far
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("popup1"))
{
other.gameObject.SetActive(false);
}
}
public class Test1 : MonoBehaviour {
[SerializeField]
GameObject otherImage; // whatever image you want to make show up temporarily.
Coroutine popupRoutine;
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("popup1"))
{
collision.gameObject.SetActive(false);
if (popupRoutine == null)
popupRoutine = StartCoroutine(ShowPopUp());
}
}
IEnumerator ShowPopUp()
{
otherImage.SetActive(true);
yield return new WaitForSeconds(2f);
otherImage.SetActive(false);
popupRoutine = null;
}
}
Assuming that your trigger code is running successfully, the above code does what you described, as I understood it.
It uses a CompareTag check to make sure the trigger is with the popup1 that you wanted, and calls a coroutine. The routine will activate the popup image, wait for 2 seconds, and then de-activate the shown image.
I included a coroutine variable, but it’s not needed, since you’re deactivating the game object in OnTriggerEnter2D. Though, it’s an example of how you could not run another routine until at least the current one is finished, the way it’s written there.