Can someone help me with my question with IENumerators in C#?

I am making my first game and I am wondering if someone can help me with a problem I have found. I have made an IENumerator for auto selling ores in my game. In the game you can hire workers and sellers to produce and sell ore for you. I have made the IEnumerator for selling the ore, but I do not know how to make it sell an amount that is less than the amount of sellers there are. So if I had 3 ores and it was selling 4 ores a second, it would take away the three and give you no money in return. I want to make it so If it was selling 4 ores a second it would sell 3 and give you the money for it instead of taking the items without a return. Does anyone have an idea on how to make it so you have a lesser amount of ores than the shops it will sell all of them even though they are not the same value?

*** edit (moved from answer)***

Thank you for your answer @JeffBrin but is there a way to code it so it is for any number not just 4? The number will fluctuate depending on how many ores you produce per second. So you may get 13 or 55 etc. every second and I want it to be if you purchase, as an example, 6 shops and want it to auto sell 13, It will go down to 7 the first second then 1 the second and then just sell the 1 in the 3rd second. But what happens when I use my code it will just take away the one. This is a big problem because the more sellers you have the higher amounts it has to be above in order to not lose all of the ore. Can you give an opinion on how to fix it with my code. Also sorry, I forgot to show the code before here it is:

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


public class AutoSell : MonoBehaviour
{
    
    public bool SellingOre = true;
    public static int CashIncrease = 1;
    public int InternalIncrease;

    void Update()
    {
        CashIncrease = GlobalShop.shopPerSec;
        InternalIncrease = CashIncrease;
        if (SellingOre == false)
        {
            SellingOre = true;
            StartCoroutine(SellTheOre());
        
        }
    }

    IEnumerator SellTheOre()
    {
        if (GlobalOres.OreCount < InternalIncrease) 
        {
            SellingOre = false;
            GlobalOres.OreCount = GlobalShop.shopPerSec;
            if (GlobalOres.OreCount < 1) ;
            {
                SellingOre = true;
            }
        }
        else
        {
            GlobalCash.CashCount += InternalIncrease;
            GlobalOres.OreCount -= GlobalShop.shopPerSec;
            yield return new WaitForSeconds(1);
            SellingOre = false;
        }        
    }  
}

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

public class PurchaseLog : MonoBehaviour {

public GameObject AutoOre;
public GameObject AutoSell;
public AudioSource playSound;

public void StartAutoOre()
{
    playSound.Play();

    AutoOre.SetActive(true);
    GlobalCash.CashCount -= GlobalMiner.minerValue;
    GlobalMiner.minerValue *= 2;
    GlobalShop.turnOffButton = true;
    GlobalMiner.turnOffButton = true;
    GlobalMiner.MinePerSec += 1;
    GlobalMiner.numberOfMiners += 1;
}

public void StartAutoSell()
{
    playSound.Play();
    AutoSell.SetActive(true);
    GlobalCash.CashCount -= GlobalShop.shopValue;
    GlobalShop.shopValue *= 2;
    GlobalShop.turnOffButton = true;
    GlobalMiner.turnOffButton = true;
    GlobalShop.shopPerSec += 1;
    GlobalShop.numberOfShops += 1;
    

}

}


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

public class GlobalShop : MonoBehaviour {

public GameObject fakeButton;
public GameObject fakeText;
public GameObject realButton;
public GameObject realText;
public int currentCash;
public static int shopValue = 50;
public static bool turnOffButton = false;
public GameObject shopStats;
public  static int numberOfShops;
public static int shopPerSec;

void Update () {
        currentCash = GlobalCash.CashCount;
    shopStats.GetComponent<Text>().text = "Shops: " + numberOfShops + " @ " + shopPerSec + " Per Second";
    fakeText.GetComponent<Text>().text = "Buy Shop = $"+ shopValue;
    realText.GetComponent<Text>().text = "Buy Shop = $"+ shopValue;
    if (currentCash >= shopValue)
    {
        fakeButton.SetActive(false);
        realButton.SetActive(true);
        
    }
    
    if (turnOffButton == true)
    {
        realButton.SetActive(false);
        fakeButton.SetActive(true);
        turnOffButton = false;
    }
}

}


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

public class GlobalOres : MonoBehaviour {

public static int OreCount;
public GameObject OreDisplay;
public int InternalOre;

void Update() {
    InternalOre = OreCount;
    OreDisplay.GetComponent<Text>().text = "Ores: " + InternalOre;
}

}


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

public class GlobalMiner : MonoBehaviour {

public GameObject fakeButton;
public GameObject fakeText;
public GameObject realButton;
public GameObject realText;
public int currentCash;
public static int minerValue = 50;
public static bool turnOffButton = false;
public GameObject minerStats;
public  static int numberOfMiners;
public static int MinePerSec;

void Update () {
    currentCash = GlobalCash.CashCount;
    minerStats.GetComponent<Text>().text = "Miners: " + numberOfMiners + " @ " + MinePerSec + " Per Second";
    fakeText.GetComponent<Text>().text = "Hire Miner = $"+ minerValue;
    realText.GetComponent<Text>().text = "Hire Miner = $" + minerValue;
    if (currentCash >= minerValue)
    {
        fakeButton.SetActive(false);
        realButton.SetActive(true);
    }
    
    if (turnOffButton == true)
    {
        realButton.SetActive(false);
        fakeButton.SetActive(true);
        turnOffButton = false;
    }
}

}

GlobalShop is class shopPerSec is int |
GlobalOres is class OreCount is int |
GlobalCash is class CashCount is int |

Also yes this is a clicker game in its beginning phase.


EDIT: I have tried your code but I keep getting into the negatives with it. I have removed unnecessary code at Lines 19-25 and I am at:

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

public class AutoSell : MonoBehaviour
{

public bool SellingOre = true;
public static int CashIncrease = 1;
public int InternalIncrease;

void Update()
{
    CashIncrease = GlobalShop.shopPerSec;
    InternalIncrease = CashIncrease;
    if (SellingOre == false)
    {
        SellingOre = true;
        StartCoroutine(SellTheOre());

    }

}

IEnumerator SellTheOre()
{
    if (GlobalOres.OreCount < InternalIncrease)
    {
        SellingOre = true;
        GlobalOres.OreCount = GlobalShop.shopPerSec;
    }
    else
        GlobalCash.CashCount += InternalIncrease;
    GlobalOres.OreCount -= GlobalShop.shopPerSec;
    yield return new WaitForSeconds(1);
    SellingOre = false;

}

}

Apparently I need to have GlobalOres.OreCount = GlobalShop.shopPerSec to stop it from going into the negatives with the code I have already.

@unity_0sdsWZad6Fwp-A I hope I understand your issue. I’m not sure if this is the most efficient way of doing it but you could do something like this with an if statement.

I think the code below should work. Just start the Coroutine and it should sell 4 at a time until you have 3 or less then it’ll sell however many you have left all while adding the price of one ore times the amount of ores being sold to your Balance with a set amount of time between sales. This is my first time answering a question I hope I understood and was helpful :)!

Edit: I think this part of your code is the issue.`

if (GlobalOres.OreCount < InternalIncrease) 
         {
             SellingOre = false;
             GlobalOres.OreCount = GlobalShop.shopPerSec;
             if (GlobalOres.OreCount < 1) ;
             {
                 SellingOre = true;
             }
         }

Why is this part of the code there:
GlobalOres.OreCount = GlobalShop.shopPerSec;

Doesn’t this just make the amount of ores equal to the number of vendors?
Why don’t you instead write:

 if (GlobalOres.OreCount < InternalIncrease) 
             {
                 SellingOre = false;
                 GlobalOres.OreCount = 0;
                 GlobalCash.CashCount += GlobalOres.OreCount;
                 if (GlobalOres.OreCount < 1) ;
                 {
                     SellingOre = true;
                 }
             }

This will add the amount of ores left to your balance while setting it to zero no matter how many there are. Let me know if it works :slight_smile: