Создайте простую универсальную утилиту ожидания, например:
public class Waiter : MonoBehaviour
{
static Waiter instance = null;
static Waiter Instance
{
get
{
if (instance == null)
instance = new GameObject("Waiter").AddComponent<Waiter>();
return instance;
}
}
private void Awake()
{
instance = this;
}
private void OnDestroy()
{
if (instance == this)
instance = null;
}
IEnumerator WaitRoutine(float duration, System.Action callback)
{
yield return new WaitForSeconds(duration);
callback?.Invoke();
}
public static void Wait(float seconds, System.Action callback)
{
Instance.StartCoroutine(Instance.WaitRoutine(seconds, callback));
}
}
Она автоматически внедряется в игру при необходимости, вам просто нужно создать скрипт, теперь в вашем OnTriggerEnter
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
collObj = other.gameObject;
collObj.SetActive(false);
Waiter.Wait(3, () =>
{
// Just to make sure by the time we're back to activate it, it still exists and wasn't destroyed.
if (collObj != null)
collObj.SetActive(true);
});
coin += 1;
chubbyScore += 50;
}
}