Yield doesn't work (he doesn't count) after I hit a powerup

using UnityEngine;
using System.Collections;
using System;
using System.Timers;

public class BoosterJump : MonoBehaviour
{

GameObject whatIHit;


public bool jumpHigh;

void Update()
{
    
}

void Start()
{
    jumpHigh = false;
    StartCoroutine(ResetJumpForce(0f));
}

void OnTriggerEnter2D(Collider2D other)
{
    jumpHigh = true;
    StartCoroutine(ResetJumpForce(2f));
    whatIHit = other.gameObject;
    // Destroy the booster
    Destroy(gameObject);
}

IEnumerator ResetJumpForce(float time)
{
    // infinity loop
    while (true)
    { 
        while (jumpHigh == false)
        {
           
            yield return null;
            
        }

        Debug.Log("boosterjump become 1500f");
        GameObject.FindGameObjectWithTag("Player2").SendMessage("BoosterJump", 1500f);
        Debug.Log(time);
        yield return new WaitForSeconds(time); // wacht 15 seconden
        Debug.Log("test yield" + time);

        if (time <= 0)
        {

            Debug.Log("Time is longer than 0 sec");
            jumpHigh = false;
            Debug.Log("boosterjump became 1000f");
            GameObject.FindGameObjectWithTag("Player2").SendMessage("BoosterJump", 1000f);
            Debug.Log("boosterjump is 1000f");
           
        }
        
    }


}

}

Vast majority of us can’t read dutch. Please comment your code in english in the future :slight_smile:

Also, try not to use while(true), it’s exceptionally bad practice, and leads to numerous curious errors. A coroutine doesn’t need to, and should be in a loop unless it’s necessary, for starters.

The reason it can’t yield, is because you tell it to return null, therefore terminating the coroutine.

Also, if you’re making a simple delay, there’s no need to use a couroutine for that. Try the following:

float delay = 15f;
float highjumped = -15f; //you can do this in start with "highjump = -delay;"

void Update()
{
  if(jumpbuttonwaspressed)
  {
    //if time of jump + delay is less than the time now...
    if(highjumped + delay < Time.time)
    {
    jump();
    highjumped = Time.time; //Save the time of the jump.
    }
  }
}

I would recommend not calling the coroutine multiple times, as that will cause it to create multiple couroutines, keep time external to the coroutine if you wish to use it as an infinite loop, that way within the coroutine check to see if time is equal to zero, if not wait, and if you can choose to skip and wait for a wait time greater than zero, or continue through without waiting.

float time = 0f;
	IEnumerator ResetJumpForce()
	{
		// infinity loop
		while (true)
		{ 
			while (jumpHigh == false)
			{
            
				yield return null;
             
			}
 
			Debug.Log("boosterjump become 1500f");
			GameObject.FindGameObjectWithTag("Player2").SendMessage("BoosterJump", 1500f);
			Debug.Log(time);

			if(time != 0f)
			{
				yield return new WaitForSeconds(time); // wacht 15 seconden
				Debug.Log("test yield" + time);
				time = 0f;
			}

			Debug.Log("Time is longer than 0 sec");
			jumpHigh = false;
			Debug.Log("boosterjump became 1000f");
			GameObject.FindGameObjectWithTag("Player2").SendMessage("BoosterJump", 1000f);
			Debug.Log("boosterjump is 1000f");
         
		}
 
 
	}

though the way i would recommend you do it to avoid sending multiple messages at a time during the frame, return null if time is equal to zero, and only use it when its requiring a wait time.

float time = 0f;
IEnumerator ResetJumpForce()
{
	Debug.Log("boosterjump became 1000f");
	GameObject.FindGameObjectWithTag("Player2").SendMessage("BoosterJump", 1000f);
	Debug.Log("boosterjump is 1000f");

	// infinity loop
	while (true)
	{ 
		while (jumpHigh == false)
		{
        
			yield return null;
         
		}

		Debug.Log("boosterjump become 1500f");
		GameObject.FindGameObjectWithTag("Player2").SendMessage("BoosterJump", 1500f);
		Debug.Log(time);

		if(time != 0f)
		{
			yield return new WaitForSeconds(time); // wacht 15 seconden
			Debug.Log("test yield" + time);
			time = 0f;

			Debug.Log("Time is longer than 0 sec");
			jumpHigh = false;
			Debug.Log("boosterjump became 1000f");
			GameObject.FindGameObjectWithTag("Player2").SendMessage("BoosterJump", 1000f);
			Debug.Log("boosterjump is 1000f");
		}
		else 
		{
			yield return null;
		}
	}
}

And that way you call it the StartCoroutine() once at the start, and can change the time as required.