Loop a function in c#.HELP!!!

I want to when i will press “Buy Dealer” button,function Deal will loop.Every 3 seconds Deal function need to add +20$ and -1g meth…

…So,when i apply this script unity is crashing…How to loop a function,please help and sorry for my broken english.

function Update()
    {
      if(dealers >=1)
       {
          if(methScript.meth >=1)
            {
               while(true)
                {
                  Deal();
                }
            }
       } 
    }
    
    IEnumerator time()
    {
      yield return new WaitForSeconds(3f);
    }

         void Deal()
{
                       moneyScript.money +=20;
                       methScript.meth -=1;
                       StartCoroutine(time());
}

Careful, you’re mixing C# and JS in your code…
It’s always best to use Coroutines instead of Update, so I wrote the basic coroutine, add your conditions and you’ll be set.

Here you go:

void Start () {
	StartCoroutine("Deal");
}

IEnumerator Deal(){
	while(true){
		moneyScript.money +=20;
		methScript.meth -=1;

		yield return new WaitForSeconds(3);
	}
}

function Update()
{
if(dealers >=1)
{
if(methScript.meth >=1)
{
while(true)
{
InvokeRepeating(“Deal”,0,3);
}
}
}
}

function Deal()
{
   moneyScript.money +=20;
   methScript.meth -=1;
}