Question by 
               nimaldev · Sep 21, 2016 at 02:54 PM · 
                c#yield waitforseconds  
              
 
              Add an value to a variable after 2 seconds
Hi i'm trying to add every 2 seconds an value to a variable, but it adds the value every Frame how can i make it work.
 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class playerController : MonoBehaviour {
 
     public Text money;
     public Text playerMoney;
     public bool test;
 
     public static int actualPlayerMoney;
      public static int actualMoney;
 
     // Use this for initialization
     void Start () {
 
         test = true;
         actualMoney = 0;
         actualPlayerMoney = 0;
         playerMoney.text = "0";
         money.text = "Money: " + actualMoney;
 
         
         
 
     }
     
     // Update is called once per frame
     void Update () {
 
         actualMoney++;
         SetText(money, "Money: ", actualMoney);
         StartCoroutine(AddMoney());
         SetText(playerMoney, "", actualPlayerMoney);
 
         if (actualPlayerMoney == 50) test = false;
 
 
 
     }
 
     void SetText(Text text, string content, int count)
     {
         text.text = content + count.ToString();
 
     }
 
     IEnumerator AddMoney()
     {
         while (true)
         {
             yield return new WaitForSeconds(5);
             Debug.Log("Sending");
             yield return new WaitForSeconds(5);
 
         }
     }
 }
 
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by OutOfRam · Sep 21, 2016 at 05:12 PM
From what I can see, the reason this is happening is because you are calling the "money++" just inside the update. you should be doing that inside your coroutine
try this
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class playerController : MonoBehaviour {
  public Text money;
  public Text playerMoney;
  public bool test;
 
  public static int actualPlayerMoney;
   public static int actualMoney;
 
  // Use this for initialization
  void Start () {
 
      test = true;
      actualMoney = 0;
      actualPlayerMoney = 0;
      playerMoney.text = "0";
      money.text = "Money: " + actualMoney;
 
      
      
 
  }
  
  // Update is called once per frame
  void Update () {
 
      //actualMoney++; // this is called once per frame so money is added once every frame
      SetText(money, "Money: ", actualMoney);
      StartCoroutine(AddMoney());
      SetText(playerMoney, "", actualPlayerMoney);
 
      if (actualPlayerMoney == 50) test = false;
 
 
 
  }
 
  void SetText(Text text, string content, int count)
  {
      text.text = content + count.ToString();
 
  }
 
  IEnumerator AddMoney()
  {
      while (true)
      {
          //yield return new WaitForSeconds(5);// this will pause for 5 seconds
         actualMoney++;
         Debug.Log("Sending");
          yield return new WaitForSeconds(2);
 
      }
  }
}
Hope this helps
Your answer
 
 
             Follow this Question
Related Questions
Trouble with Co routines & WaitForSeconds 0 Answers
Trying to make dash energy system... 1 Answer
Yield wait for seconds not working at all? 2 Answers
yield ends method 0 Answers
Getting a variable timed Interval with coroutine and yield? 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                