Coroutine Not Looping

Coroutine in my script is not looping. The cash only goes once and then it stops. I’ve looked at many posts but they dont seem to solve my problem. Any help?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BuyItems : MonoBehaviour
{
    public int cash = 0;
    public Text cashText;

    private void Update()
    {
        cashText.text = "$" + cash;
    }

    public void BuyItem(int itemnum)
    {
        while(itemnum == 1 && cash >= 0)
        {
            StartCoroutine(SocialMedia());
        }

        while(itemnum == 2 && cash >= 5)
        {
            cash -= 5;
            StartCoroutine(Flyers());
        }

        while(itemnum == 3 && cash >= 10)
        {
            cash -= 10;
            StartCoroutine(Posters());
        }

        while(itemnum == 4 && cash >= 50)
        {
            cash -= 50;
            StartCoroutine(Emails());
        }

        while(itemnum == 5 && cash >= 125)
        {
            cash -= 125;
            StartCoroutine(Catalog());
        }

        while(itemnum == 6 && cash >= 500)
        {
            cash -= 500;
            StartCoroutine(Radio());
        }

        while(itemnum == 7 && cash >= 1000)
        {
            cash -= 1000;
            StartCoroutine(Youtube());
        }

        while(itemnum == 8 && cash >= 5000)
        {
            cash -= 5000;
            StartCoroutine(BrandPartnership());
        }
    }

    IEnumerator SocialMedia()
    {
        yield return new WaitForSeconds(50);
        cash++;
    }

    IEnumerator Flyers()
    {
        yield return new WaitForSeconds(25);
        cash++;
    }

    IEnumerator Posters()
    {
        yield return new WaitForSeconds(15);
        cash++;
    }

    IEnumerator Emails()
    {
        yield return WaitForSeconds(5);
        cash++;
    }

    IEnumerator Catalog()
    {
        yield return new WaitForSeconds(1);
        cash++;
    }

    IEnumerator Radio()
    {
        yield return new WaitForSeconds(1);
        cash +=10;
    }

    IEnumerator Youtube()
    {
        yield return new WaitForSeconds(1);
        cash +=25;
    }

    IEnumerator BrandPartnership()
    {
        yield return new WaitForSeconds(1);
        cash +=100;
    }
}

The issue you’re facing with the coroutine not looping is due to the way you are using while loops in your BuyItem method. Coroutine execution and while loops don’t work well together in this context because they both operate on the same thread. When you start a coroutine, it runs asynchronously and doesn’t block the main thread, but your while loops are continuously checking conditions on the main thread, effectively blocking the execution of the coroutine.

To achieve the desired behavior of buying items and having their effects applied repeatedly based on a certain time interval, you should redesign your code to use a different approach. You can use a coroutine to manage the item effects and their timing, and the Update method to handle user input and initiate the buying process.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BuyItems : MonoBehaviour
{
    public int cash = 0;
    public Text cashText;

    private void Update()
    {
        cashText.text = "$" + cash;
    }

    public void BuyItem(int itemnum)
    {
        switch (itemnum)
        {
            case 1:
                if (cash >= 0)
                    StartCoroutine(StartItemEffect(SocialMedia, 50));
                break;
            case 2:
                if (cash >= 5)
                {
                    cash -= 5;
                    StartCoroutine(StartItemEffect(Flyers, 25));
                }
                break;
            // Add cases for other items
            default:
                break;
        }
    }

    private IEnumerator StartItemEffect(System.Action itemEffect, float interval)
    {
        while (true)
        {
            itemEffect.Invoke();
            yield return new WaitForSeconds(interval);
        }
    }

    private void SocialMedia()
    {
        cash++;
    }

    private void Flyers()
    {
        cash++;
    }

    // Add methods for other items

    // ... Rest of the script
}