Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Help needed with Update and Wait function.

Discussion in 'Scripting' started by ApexShadow, Jun 13, 2016.

  1. ApexShadow

    ApexShadow

    Joined:
    May 31, 2016
    Posts:
    5
    So I have a function that when I click the button it starts giving me money, I originally placed the money giving code in the update but that gave me money so fast. I want it to have a 5 second delay before giving me more money.

    Here is all my code.
    Sorry if some of it doesnt make sense, I am going to revise it when I get out the testing stage.
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class BuildBuildingTileOne : MonoBehaviour {
    6.     public GameObject TileOneBuildButton;
    7.     public GameObject TestBuilding;
    8.     public Text MoneyText;
    9.     public bool TestBuildingActive;
    10.     public int Money = 50;
    11.     // Use this for initialization
    12.     void Start () {
    13.         TestBuildingActive = false;
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update () {
    18.         if (TestBuildingActive == false)
    19.         {
    20.             return;
    21.         }
    22.         else if (TestBuildingActive == true)
    23.         {
    24.             MoneyUpdate();
    25.         }
    26.         MoneyText.text = "Money:" + Money;
    27.     }
    28.  
    29.     public void OnMouseDown()
    30.     {
    31.         TileOneBuildButton.SetActive(true);
    32.     }
    33.  
    34.     public void BuildBuilding()
    35.     {
    36.         TestBuildingActive = true;
    37.         TestBuilding.transform.position = new Vector3(5,26,0);
    38.     }
    39.     public void MoneyUpdate()
    40.     {
    41.         Money += 1;
    42.         StartCoroutine("WaitFiveSeconds");
    43.     }
    44.     IEnumerator WaitFiveSeconds()
    45.     {
    46.         yield return new WaitForSeconds(5);
    47.         Update();
    48.     }
    49. }
    50.  
     
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,626
    Replace MoneyUpdate(); with Invoke("MoneyUpdate", 5); and delete the coroutine completely.
     
  3. ApexShadow

    ApexShadow

    Joined:
    May 31, 2016
    Posts:
    5
    It worked for the first delay but then it went back to every frame giving me money. I hope I didn't misunderstand anything,
    I did leave this in, was I supposed to delete it?
    Code (CSharp):
    1. IEnumerator WaitFiveSeconds()
    2.     {
    3.         yield return new WaitForSeconds(5);
    4.         Update();
    5.     }
     
  4. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,626
    I'm guessing it's because TestBuildingActive stays true, so it keeps invoking every frame.
     
  5. ApexShadow

    ApexShadow

    Joined:
    May 31, 2016
    Posts:
    5
    Do you have any Idea how I would fix that?
     
  6. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,626
    Watch some tutorials...

    Any way...

    Code (csharp):
    1.  
    2. if (TestBuildingActive == true)
    3. {
    4.     Invoke("MoneyUpdate",5);
    5.     TestBuildingActive = false;
    6. }
     
  7. LTK

    LTK

    Joined:
    Jul 16, 2015
    Posts:
    24
  8. nelson218

    nelson218

    Joined:
    Jun 7, 2016
    Posts:
    11
    Try these

    Code (CSharp):
    1. float currentTime = .0f;
    2. float timeAfterAddMoney = 5.0f;
    3.  
    4. void Update()
    5. {
    6.    if(Input.getMouseButtonDown(0))
    7.    {
    8.        addMoney();
    9.        currentTime = time.Time + timeAfterAddMoney;//Works like timer
    10.    }
    11.  
    12.    if(Input.getMouseButton(0))
    13.    {
    14.         if(currentTime < time.Time)
    15.         {
    16.              addMoney();
    17.              currentTime = time.Time + timeAfterAddMoney;
    18.         }
    19.    }
    20. }