- Home /
How to increase Value of for example "gold" every second?
Hello, thank you for taking time for my question!
I want to create a small game that has a small simple economy. Every second, for example, the value ' Gold ', which is represented as text, has to be increased by 1 every second.
I imagine this:
At the top left corner is ' Gold: 15 '
After that, the value ' 15 ' should be counted up every second.
Would anyone have a C# script for me please?
Answer by Sazails · Jul 27, 2018 at 05:35 PM
using UnityEngine;
using UnityEngine.UI;
public class GoldManager : MonoBehaviour
{
public int GoldValue = 15;
public int Power = 5;
public int DelayAmount = 1; // Second count
public Text GoldValueText;
public Text PowerText;
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;
}
}
}
By now you should get the main idea, giving code is not the best way to learn all the time but it'll be enough for now :)!
Thank you also for your reply! This community is so nice! I'm going to try your solution. A big thank you too! Thanks, that's exactly what I was looking for!
Just how can I change the text "Gold value: " For example to "power: ". I just tried this in the script, but it doesn't work:
GoldValueText.text = "Power: " + GoldValue;
What is power going to be? If it is going to be a number that is going to contain decimal place use float, if not use int. I am going to use integer (int). outside of the update() function make a variable, in your case I'd do
public int GoldValue = 15;
public int Power;
The same as the text, this is displayed:
Gold Value: 15
But I want to know if I can change this so easily for example to power Volue: 15.
So I tried to change it from
GoldValueText.text = "Gold value: " + GoldValue;
to
GoldValueText.text = "Power: " + GoldValue;
This is one of the simplest things, but somehow something is bewitched.
if (Timer >= DelayAmount)
Timer = 0f;
is better as:
if (Timer >= DelayAmount)
Timer -= Timer;
or you're throwing away the surplus every time the code executes the block
Answer by David_Rios · Jul 27, 2018 at 04:57 PM
What you can do is have a timer that increments a gold value every second.
Script:
public int goldValue = 15;
private float timer = 0f;
public float delayAmount;
void Update()
{
timer += Time.deltaTime;
if (timer >= delayAmount)
{
timer = 0f;
goldValue++;
}
}
You can preset your custom delay amount e.g. 1.0f in the editor.
You can also use Coroutines, but I feel that this is a more efficient and simpler option.
Thank you for responding so nicely and quickly! I hope it works! Big thanks to you!
No problem! This is untested, so I hope it works too. :)
$$anonymous$$ay you please mark this as the definite answer if it does work? Thank you!
Can you tell me how to apply this script? Where do I put it? And how can you use Goldvalue if you didn't tell the script what it is and where it is?
Sorry, I'm a layman in program$$anonymous$$g. So apologies for those stupid questions.
Answer by cryingwolf85 · Jul 31, 2018 at 01:09 AM
The selected answer works, but I would much prefer a Coroutine in this situation, especially when you are dealing with time. Also great in terms of maintainability and readability.
private int GoldValue = 15;
private float TimeSeconds = 1;
private void Start() {
StartCoroutine(SpawnTimer());
}
private IEnumerator SpawnTimer() {
while (true) {
yield return new WaitForSeconds(TimeSeconds);
GoldValue++;
}
}
Your answer
Follow this Question
Related Questions
Problems with finger count. 0 Answers
Increase/Decrease Over Time, Not Frames 1 Answer
How can i randomly increase/decrease a value over time? 1 Answer
c# List not fully clearing 0 Answers
polycount IOS / Android games? 2 Answers