How to make function last for X seconds?
I have a script where when my boomerang collides with an enemy, the enemy is disabled. However, the enemy is only set active for about half of a second. I want the enemy to be disable for 5 seconds (X seconds). Any help would be appreciated
Sorry, I'm new to Unity would like some guidance! Thanks!
void OnCollisionEnter (Collision other) {
if (other.gameObject.tag == "Enemy")
{
other.gameObject.SetActive (false);
}
}
Answer by Jason2014 · Dec 16, 2016 at 07:59 PM
Remember: If you want behavior with delay / timer you have to insert it in "IEnumerator" and call it through "StartCoroutine". Try this code:
void OnCollisionEnter (Collision other)
{
if (other.gameObject.tag == "Enemy")
{
StartCoroutine (DisableEnemy (other.gameObject));
}
}
IEnumerator DisableEnemy (GameObject enemy)
{
enemy.SetActive (false);
yield return new WaitForSeconds (5f);
enemy.SetActive (true);
}
Your answer
Follow this Question
Related Questions
How can i restart a script on collision ? 1 Answer
Unity 2D colliders not triggering,Unity 2D collider not triggering 0 Answers
How to snap a placeable object on wall & move on to other walls of a room? 0 Answers
How to detect collision of two moving characters? 1 Answer
Changing light color between players when they collide (BOLT) 0 Answers