#Delay Timer not working
1 messages · Page 1 of 1 (latest)
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.
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);
}
}
CoinManager.AddMoney(GoldValue);
And where is this defined?
sorry about that
this is the updated code
Money manager is a singleton and I am calling the addcoin method in the coins script
you misspelled coin btw
oh sorry about that 😢
Did you test this code after changing it?
to MoneyManager.instance.AddCoin(GoldValue);
yeah
The delay is working perfectly
but when I reference another script it does not work for that script
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
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
}
oh I see