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;
}
}