- Home /
Making coins reappear after a set time
Hey Guys!
I have a game in progress and when the character moves over the coins, they are turned to false and the counter increases. all of that works no problem. what im wondering is how do i make the coins reappear after a set time? im using C#
void OnTriggerEnter (Collider other)
{
if (other.gameObject.CompareTag ("Coin"))
other.gameObject.SetActive (false);
count = count + 1;
SetCoinCounter ();
}
The best way to do it is with a Coroutine, but that is a little advanced and not really necessary unless you are willing to take the time to learn.
The other way is to have a timeElapsed variable on your Coin that keeps incrementing Time.deltaTime whenever this coin deactivates (perhaps by testing if it's active or not). Once timeElapsed is greater than the desired time you can re-activate the coin and reset the timeElapsed back to 0.
Answer by Cherno · May 22, 2015 at 10:46 PM
Use a CoRoutine a suggested.
public float resetDelay = 3f;
void OnTriggerEnter (Collider other)
{
if (other.gameObject.CompareTag ("Coin"))
other.gameObject.SetActive (false);
StartCoroutine(ResetCoin(other.gameObject, resetDelay ));
count = count + 1;
SetCoinCounter ();
}
private IEnumerator ResetCoin(GameObject coinObject, float delay) {
yield return new WaitForSeconds(delay);
coinObject.SetActive (true);
}
Your answer