I’m trying to use helper function in order to make the function pause for few seconds but it doesn’t execute the wait function.
That’s the code I’ve been using:
public class Triggers : MonoBehaviour {
IEnumerator wait(float seconds) {
Debug.Log("In wait");
yield return new WaitForSeconds(seconds);
Debug.Log("after wait");
}
void OnTriggerEnter(Collider _collider)
{
Debug.Log("Destroy");
gameObject.SetActive(false);
Debug.Log("Before wait");
wait(5);
Debug.Log("activate");
gameObject.SetActive(true);
}
}
I’d appreciate some help.
You need to explicitly use StartCoroutine(wait(5));
But the way you set it up you wouldnt notice any activity because the code isnt paused at StartCoroutine but in the coroutine itself so the code after StartCoroutine will be executed but the coroutine itself will be paused. Hope you understand me 
Changes you need to make:
Use StartCoroutine(wait(5));
and put gameObject.SetActive(…) in the coroutine infront and behind the yield statement instead of in OnTrigger
I think i got you and that’s what I did :
IEnumerator wait(float seconds) {
Debug.Log("In wait");
gameObject.SetActive(false) ;
yield return new WaitForSeconds(seconds);
Debug.Log("after wait");
GameObject go = GameObject.Find("Cube");
go.SetActive(true);
}
void OnTriggerEnter(Collider _collider)
{
Debug.Log("Destroy");
Debug.Log("Before wait");
StartCoroutine(wait(5));
Debug.Log("activate");
}
But it printed only : Destroy , Before wait , In wait and active . It skipped the after wait , any idea what’s wrong ?
The gameobject is inactive and with it every component so id guess it is stuck until you manually reactivate it again.
Thanks , figured it out already .
I solved it by just deactivating the child object which is the actual “physical” object I wanted to hide on collision with the invisible parent object.
Debug.Log ("In wait");
GameObject go = GameObject.Find ("Cube");
go.SetActive(false);
yield return new WaitForSeconds(seconds);
Debug.Log ("after wait");
go.SetActive (true);
So now the parent object stays active, counting the time and the physical object “Cube” dis appears and reappears after n seconds.