WaitForSeconds doesn't work when seconds is greater than 2
I'm writing a simple script to disable a ball after it gets hit by another GameObject and re-enable the ball after 5 seconds. When I set WaitForSeconds to be 1 or 2 seconds, it works fine, but anything above that and it fails. I've tried putting a Debug statement after the yield, but it simply never gets triggered. Any help will be appreciated!
using UnityEngine;
using System.Collections;
public class DetectHit : MonoBehaviour {
public AudioSource explosionAudio;
private GameObject sphere;
void OnCollisionEnter(Collision col) {
if (col.gameObject.tag == "sphere") {
sphere = col.gameObject;
StartCoroutine(ballEffect());
}
}
private IEnumerator ballEffect() {
explosionAudio.Play();
sphere.SetActive(false);
yield return new WaitForSeconds(5);
sphere.SetActive(true);
}
}
And you've got no other scripts that could be, say, Destroy() ing the object to which this script us attached after 3 seconds?
Oh my gosh, now that you mention that, I think I know what I'm doing wrong. I don't have access to a computer right now, but I think the way I'm destroying the GameObject that hits the ball is affecting the balls themselves. Thanks for the heads up!
Your answer
![](https://koobas.hobune.stream/wayback/20220612113409im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Double Input.GetButton problem. 1 Answer
End Game (Stop spawning) with OnTriggerEnter or OnCollisionEnter? 0 Answers
SetActive() trouble 1 Answer
Deactivate gameobject childen in the Game Manager Script 0 Answers
Endlessly Spinning Drone 1 Answer