Make a laser flickering.

Is this a good way to do it? I just set the laser on and off less than 100. I mean it is working, but it will stop once it reach 100. So yeah…

void Start()
{
    StartCoroutine(LaserFlickering());
}

IEnumerator LaserFlickering()
{
    while (laserOnOff < 100)
    {
        yield return new WaitForSeconds(2);
        laser.SetActive(false);
        yield return new WaitForSeconds(2);
        laser.SetActive(true);
        laserOnOff += 1;
    }
}

}

You could use a delay and a toggle for the activation

Something like this should do

if (m_Delay < m_MaxDelay)
        {
            m_Delay += Time.deltaTime;
        }

        else
        {
            m_Delay = 0;
            m_Object.SetActive(!m_Object.activeSelf); //Toggles activation
        }