I have to make a code where the cube disappears in a couple of seconds after the player touches it. But I have no idea how to make it because StartCoroutine(name()); works only in IEnumerator name(){} like this:
void Start()
{
StartCoroutine(waiter());
}
IEnumerator waiter()
{
//Wait for 4 seconds
yield return new WaitForSeconds(4);
}
but I need to wait in private void OnCollisionEnter(Collision collision) {} function.
I can't do something like this:
void start()
{
StartCoroutine(Text());
}
IEnumerator Text()
{
private void OnCollisionEnter(Collision collision)
{
yield return new WaitForSeconds(3);
StartCoroutine(Text());
rd = GetComponent<Renderer>();
rd.enabled = false;
}
}
How to make a cooldown in OnCollisionEnter function?
Invokehas its good side and also a bad one: It is not cancelable. The Coroutine can still be stopped if e.g. the component is disabled in the meantime (let's e.g. say you open a Pause menu). TheInvokewill be invoked after the time passed no matter what ;)