Modulus with float

Im trying to do a coroutine that sets the character to hurt mode for a persiod. During this period i want it to flash. Im not familiar with modulus but i tried to use it to flip the sprite renderer every 10 or so cycles. But i get really weird values, is it cause its a float? How should i do this in unity?

public IEnumerator Hurt_cr (float sec, Vector2 attackPosition, float force)
		{
				isHurt = true;
				
				AddForceAtPosition (attackPosition, force);

				SpriteRenderer sprite = GetComponent< SpriteRenderer> ();
				float t = Time.deltaTime;
				
				while (t < sec) {
						
						if ((t % 10) == 0) {
								
								print ("sprite on :  " + sprite.enabled);
								sprite.enabled = !sprite.enabled;
						}
			
						yield return null;
						t += Time.deltaTime;
				}

				isHurt = false;
		}

Modulus using floats can be achieved using Mathf.Repeat. Alternatively you can try rounding the number first, or flooring it, depending on the situation.

I got a problem with the modulo of two small float values. The Mathf.Repeat function was not viable for me, so i had to find an other solution. Here is my solution:

 moduloResult = smallFloatA % smallFloatB; 

doesn’t work for me. But it should be the same as this:

 moduloResult = smallFloatA / smallFloatB - (int)(smallFloatA / smallFloatB); 

I tried it in my gamecode and it worked well.

Sometimes, modulus won’t work effectively with time and float… especially when your script is not on your default/start Scene and done some Application.LoadLevel(); or using UnityEngine.SceneManagement; SceneManager.LoadScene(); in game-play. Weird enough I have experience that…

Here what I suggest instead, if i make it right:

private int i; //declared private i variable...

void Start ()
{
    i = 0; //initiate 'i' to zero...
     /* or initiate 'i' to one? if you want to wait/delay for 10 sec to apply your function */
}
...

/*...Your codes here...*/

...

while (t < sec) { //'t' less than 'sec', this means the duration?
    
    /* commented out...
    if ((t % 10) == 0) { //As what I experience, sometimes modulus don't work well when dealing with floats (especially run-time), but works perfectly with int...
    } */

    
    if (t >= 10 * i) {  //use this line instead, if 't' is equal or slight more than 10: it become true, if it succeed, then 'i++;' apply to multiply the 10 for another 10 seconds...
        
        print ("sprite on :  " + sprite.enabled);
        sprite.enabled = !sprite.enabled;
        i++;
    }
    
    yield return null;
    t += Time.deltaTime;
}
...