I’m currently trying to have an item which will add one kind of resource per second (which is working) but deduct a different resource per second (which isn’t working) - they’re both coded using IEnumerators and coroutines, but for some reason it wont deduct.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MoneyPerSec : MonoBehaviour
{
public Text mpsDisplay;
public moneyPerClick money;
public CafeItemManager[] items;
public FoodClick food;
public float FCPS;
// Use this for initialization
void Start()
{
StartCoroutine(AutoTick());
}
// Update is called once per frame
void Update()
{
mpsDisplay.text = GetMoneyPerSec() + " money/sec";
}
public float GetMoneyPerSec()
{
float tick = 0;
foreach (CafeItemManager item in items)
{
tick += item.count * item.autoIncome;
}
return tick;
}
public void AutoMoneyPerSec()
{
money.money += GetMoneyPerSec();
}
IEnumerator AutoTick()
{
while (true)
{
AutoMoneyPerSec();
yield return new WaitForSeconds(1);
}
}
public IEnumerator AutoDeduct() //This is where the issue seems to be
{
foreach (CafeItemManager item in items)
{
foodCostPerSec();
yield return new WaitForSeconds(1);
}
}
private void foodCostPerSec()
{
food.food -= FCPS; //Problem
}
}
The script is attached to all of the items - and a FCPS value is assigned to each of them, the script is also assigned to the game Manager just like my other scripts that function in the same way and in the inspector the food resource script is assigned to each of them so it shouldn’t have issues accessing the food resource to deduct it. The money is added every second, but the food resource is still showing the same amount per second as it was previously.
I can’t see anywhere on the script that is starting a coroutine for ‘AutoDeduct’ - it doesn’t seem to be called anywhere. Is it being called from somewhere else? Also, the autodeduct coroutine would finish because it doesn’t have a ‘while (true)’ around it. I think it should look like this:
public IEnumerator AutoDeduct() //This is where the issue seems to be
{
while( true )
{
foreach (CafeItemManager item in items)
{
foodCostPerSec( item );
}
yield return new WaitForSeconds(1);
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MoneyPerSec : MonoBehaviour
{
public Text mpsDisplay;
public moneyPerClick money;
public CafeItemManager[] items;
public FoodClick food;
public float FCPS;
public Text foodCPS;
// Use this for initialization
void Start()
{
StartCoroutine(AutoTick());
StartCoroutine(AutoDeduct());
}
// Update is called once per frame
void Update()
{
mpsDisplay.text = GetMoneyPerSec() + " money/sec";
foodCPS.text = FCPS + " food cost/sec";
}
public float GetMoneyPerSec()
{
float tick = 0;
foreach (CafeItemManager item in items)
{
tick += item.count * item.autoIncome;
}
return tick;
}
public void AutoMoneyPerSec()
{
money.money += GetMoneyPerSec();
}
IEnumerator AutoTick()
{
while (true)
{
AutoMoneyPerSec();
yield return new WaitForSeconds(1);
}
}
public IEnumerator AutoDeduct()
{
while (true)
{
foodCostPerSec();
yield return new WaitForSeconds(1);
}
}
private void foodCostPerSec()
{
FCPS -= food.food;
}
}
I’ve updated the script to include the while true function, however, now with the audodeduct coroutine/IEnumerator, for some reason it is adding 10,000 food per sec, as opposed to subtracting 10 per second. It also increases by about 10k with every add. The button that displays the cost per second correctly displays ‘-’ but again shows at 10,000 then 20,0000 and so on. It is also now not adding any money at all.
what is FCPS initialized too in the inspector?
Where is food.food getting its value from It looks like food is some custom FoodClick class you made. Your code seems fine. FCPS starts at some initial value… Then every second your Autodeduct is running the line FCPS-=food.food. So something must off in your starting values and whatever food.food equals.
FCPS is attached to 6 items in the inspector, each one has a different value, and food.food is the foodClick script (the starting value of food per click is 1 which can obviously increase depending on how many upgrades the player has purchased. The lowest FCPS is 10, then 20, 30, 40, 50 and 100.
Each item controls the cost themselves.
Well a few things are confusing about your first statement. You say FCPS is attched to 6 items in the inspector yet you only have 1 float in this script. Do you have this script attached to 6 different objects? If not how are you getting the initial value from those 6 items into this scripts one FCPS float value?
Secondly, in an earlier post you mentioned that AutoDeduct was being called when you built something. Now you have it being called from this scripot’s start function. DId make sure to take out the calls to AutoDeduct from the building script?
This script is attached to 6 buttons, and each button is set up with their own values for wood, food and money. The food script is exactly the same (without the autodeduct at the moment) and each button has their own values assigned in the inspector, then a game manager that has the item manager script attached with all the items attached to handle the adding and subtracting etc.
I originally had the audodeduct start immediately “OnPurchase” but that didn’t seem to work so I shuffled the code to try and get it working, but that didn’t work. In theory it should start when an item (the button) is pressed. But yes I did remove the code from the previous script.
This means there are 6 copies of this script running. Each of them is a separate object in your program. They all are exact copies of each other, but any code in that script is being run 6x per frame.
Okay - but that’s how several of the other scripts are being ran and they don’t have any issues. The issue is that part of the script isn’t being run! And the scripts are only activated when an item is purchased. So to begin with this particular script isn’t even being run.