Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Richyman · Jul 27, 2018 at 04:41 PM · valuecountincreaseseconds

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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 :)!

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Richyman · Jul 27, 2018 at 05:39 PM 0
Share

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!

avatar image Richyman · Jul 27, 2018 at 05:50 PM 0
Share

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;

avatar image Sazails Richyman · Jul 27, 2018 at 05:52 PM 0
Share

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;

avatar image Richyman Sazails · Jul 27, 2018 at 05:56 PM 0
Share

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.

Show more comments
Show more comments
avatar image ordinov · May 25 at 06:05 AM 0
Share
 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

avatar image
1

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Richyman · Jul 27, 2018 at 05:00 PM 0
Share

Thank you for responding so nicely and quickly! I hope it works! Big thanks to you!

avatar image David_Rios Richyman · Jul 27, 2018 at 05:09 PM 1
Share

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!

avatar image Richyman David_Rios · Jul 27, 2018 at 05:22 PM 0
Share

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.

Show more comments
avatar image
1

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++;  
         }
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

93 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Increase/Decrease Over Time, Not Frames 1 Answer

How can i randomly increase/decrease a value over time? 1 Answer

Problems with finger count. 0 Answers

Countdown little help...! 1 Answer

Array.Count returning 0 out of the foreach and 4 inside 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges