Hello, I am having some trouble with having my game wait for a powerup to finish and then deactivating it when the time is up. The basic idea is when the powerup collides with the player, it activates the powerup, waits for x amount of seconds, and deactivates the powerup. However, with the code I have, it is only activating and never deactivating. The code after my WaitForSeconds(3) is never ran and i do not know why.
public void OnCollisionEnter2D( Collision2D collision ){
if (collision.gameObject.tag == "Player") {
if(isActive){
isActive = false;
gameObject.renderer.enabled = false;
StartCoroutine( activatePowerup( collision.gameObject ) );
}else;
Debug.Log ("Hit player");
}else if (collision.gameObject.tag == "Ground") {
Destroy(gameObject);
}
}
public IEnumerator activatePowerup( GameObject player ){
player.GetComponent<Player>().setAttackDelaySeconds (0.25f);
Debug.Log ("Powerup Activated");
yield return new WaitForSeconds(3);
Debug.Log ("Finisehd waiting");
deactivatePowerup ( player );
}
public void deactivatePowerup( GameObject player ){
player.GetComponent<Player> ().setAttackDelaySeconds (0.75f);
Debug.Log ("Powerup Finished");
Destroy (gameObject);
}