Ways to Unity 15: Use Threading or Timer or Coroutine to show or hide an object
What we did
When we get powerup, we show the powerupIndicator
, then we start a coroutine.
The coroutine function will wait for 7 seconds, then hide the powerupIndicator
.
The Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public GameObject powerupIndicator;
void Start()
{
}
void Update()
{
}
IEnumerator PowerupCountdownRoutine() {
yield return new WaitForSeconds(7);
powerupIndicator.SetActive(false);
}
private void OnTriggerEnter(Collider other) {
if (other.CompareTag("Powerup"))
{
powerupIndicator.SetActive(true);
StartCoroutine(PowerupCountdownRoutine());
}
}
}
The man behind this article
https://yingshaoxo.blogspot.com/2020/12/ways-to-unity-15-use-threading-or-timer.html