#Delay Timer not working

1 messages · Page 1 of 1 (latest)

waxen axle
#

I have a created a simple timer for my money script which increments the money by a second but when I reference the money manager to add the money to our total money, the timer does not still apply to the manager script. please help

gentle ruinBOT
#

An image of your code is not helpful

When asking a question about a problem with code, people who are volunteering to help need the text of the code. Unless you are asking about your IDE - and not the code itself - images of the code are not an acceptable substitute.
Source: https://idownvotedbecau.se/imageofcode

Please send your code as a codeblock. If you don't know how to send a codeblock, type []cb.

waxen axle
#
 public class MoneyManager : MonoBehaviour
{

    public TextMeshProUGUI coinText;
    public int initialCoins;
    public static MoneyManager instance;
    public TextMeshProUGUI mainCoinText;
    public int mainCoin;
    

    public void Awake()
    {
        instance = this;
    }
    // Start is called before the first frame update
    void Start()
    {
        mainCoin = PlayerPrefs.GetInt("NumberOfCoins", 0);
        initialCoins = 0;
        // Display the coins
        UpdateCoin(mainCoin);
       
    }


  
    public void AddCoin(int coin)
    {
        
        initialCoins = coin;
        mainCoin += initialCoins;
        Debug.Log(initialCoins);

        SaveCoin(mainCoin);
        UpdateICoin(initialCoins);
        UpdateCoin(mainCoin);



        
    }

    private void UpdateICoin(int initialCoins)
    {
        coinText.text = initialCoins.ToString();
    }

   
    public void SaveCoin(int coin)
    {
        PlayerPrefs.SetInt("NumberOfCoins", coin);
    }

    public void UpdateCoin(int coin)
    {
        mainCoinText.text = coin.ToString();
    }
   
}
#
public class Coiin : MonoBehaviour
{

    public int GoldValue = 15;
    public int Power = 5;
    public int DelayAmount = 1; // Second count
  

    protected float Timer;

    void Update()
    {
        Timer += Time.deltaTime;

        if (Timer >= DelayAmount)
        {
            Timer = 0f;
            GoldValue++; // For every DelayAmount or "second" it will add one to the GoldValue
           // GoldValueText.text = "Gold value: " + GoldValue;
            //PowerText.text = "Power: " + Power;


        }
        MoneyManager.instance.AddCoin(GoldValue);
    }

}
near pollen
#
CoinManager.AddMoney(GoldValue);

And where is this defined?

waxen axle
#

sorry about that

#

this is the updated code

#

Money manager is a singleton and I am calling the addcoin method in the coins script

near pollen
#

you misspelled coin btw

waxen axle
#

oh sorry about that 😢

near pollen
#

Did you test this code after changing it?

#

to MoneyManager.instance.AddCoin(GoldValue);

waxen axle
#

yeah

#

The delay is working perfectly

#

but when I reference another script it does not work for that script

near pollen
#

what do you mean

#

"does not work for that script"

waxen axle
#

ok

#

so the delay is on the coin script right?

#

it is working fine as it is

#

but when I reference the money manager script to add the current coins we have, it does not add that delay anymore

#

it just adds per frame

near pollen
#

Well that's because you're telling it to do that. You're not gating the call to AddCoin

#

It's not part of the timer if statement

#

It's just inside Update. So of course it's going to do it every frame

#

i.e.

void Update()
{
    Timer += Time.deltaTime;

    if (Timer >= DelayAmount)
    {
        Timer = 0f;
        GoldValue++;
    }

    MoneyManager.instance.AddCoin(GoldValue); // <--- this is unconditional
}
waxen axle
#

oh I see

waxen axle
#

It works

#

The issue now is I have to coin values : initial coins and main coins. The initial coins is our current money while the main coins is our total money

#

the initial coins is updating well